package entities

import (
	"time"

	"github.com/google/uuid"
)

type LetterOutgoingStatus string

const (
	LetterOutgoingStatusDraft           LetterOutgoingStatus = "draft"
	LetterOutgoingStatusPendingApproval LetterOutgoingStatus = "pending_approval"
	LetterOutgoingStatusApproved        LetterOutgoingStatus = "approved"
	LetterOutgoingStatusSent            LetterOutgoingStatus = "sent"
	LetterOutgoingStatusArchived        LetterOutgoingStatus = "archived"
)

type LetterOutgoing struct {
	ID                    uuid.UUID            `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	LetterNumber          string               `gorm:"uniqueIndex;not null" json:"letter_number"`
	ReferenceNumber       *string              `json:"reference_number,omitempty"`
	Subject               string               `gorm:"not null" json:"subject"`
	Description           *string              `json:"description,omitempty"`
	PriorityID            *uuid.UUID           `json:"priority_id,omitempty"`
	ReceiverInstitutionID *uuid.UUID           `json:"receiver_institution_id,omitempty"`
	IssueDate             time.Time            `json:"issue_date"`
	Status                LetterOutgoingStatus `gorm:"not null;default:'draft'" json:"status"`
	ApprovalFlowID        *uuid.UUID           `json:"approval_flow_id,omitempty"`
	CreatedBy             uuid.UUID            `gorm:"not null" json:"created_by"`
	CreatedAt             time.Time            `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt             time.Time            `gorm:"autoUpdateTime" json:"updated_at"`
	DeletedAt             *time.Time           `gorm:"index" json:"deleted_at,omitempty"`

	// Relations
	Priority            *Priority            `gorm:"foreignKey:PriorityID" json:"priority,omitempty"`
	ReceiverInstitution *Institution         `gorm:"foreignKey:ReceiverInstitutionID" json:"receiver_institution,omitempty"`
	Creator             *User                `gorm:"foreignKey:CreatedBy" json:"creator,omitempty"`
	ApprovalFlow        *ApprovalFlow        `gorm:"foreignKey:ApprovalFlowID" json:"approval_flow,omitempty"`
	Recipients          []LetterOutgoingRecipient    `gorm:"foreignKey:LetterID" json:"recipients,omitempty"`
	Attachments         []LetterOutgoingAttachment   `gorm:"foreignKey:LetterID" json:"attachments,omitempty"`
	Approvals           []LetterOutgoingApproval     `gorm:"foreignKey:LetterID" json:"approvals,omitempty"`
	Discussions         []LetterOutgoingDiscussion   `gorm:"foreignKey:LetterID" json:"discussions,omitempty"`
	ActivityLogs        []LetterOutgoingActivityLog  `gorm:"foreignKey:LetterID" json:"activity_logs,omitempty"`
}

func (LetterOutgoing) TableName() string { return "letters_outgoing" }

type LetterOutgoingRecipient struct {
	ID                   uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	LetterID             uuid.UUID `gorm:"type:uuid;not null" json:"letter_id"`
	RecipientName        string    `gorm:"not null" json:"recipient_name"`
	RecipientEmail       *string   `json:"recipient_email,omitempty"`
	RecipientPosition    *string   `json:"recipient_position,omitempty"`
	RecipientInstitution *string   `json:"recipient_institution,omitempty"`
	IsPrimary            bool      `gorm:"default:false" json:"is_primary"`
	CreatedAt            time.Time `gorm:"autoCreateTime" json:"created_at"`
}

func (LetterOutgoingRecipient) TableName() string { return "letter_outgoing_recipients" }

type LetterOutgoingAttachment struct {
	ID         uuid.UUID  `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	LetterID   uuid.UUID  `gorm:"type:uuid;not null" json:"letter_id"`
	FileURL    string     `gorm:"not null" json:"file_url"`
	FileName   string     `gorm:"not null" json:"file_name"`
	FileType   string     `gorm:"not null" json:"file_type"`
	UploadedBy *uuid.UUID `json:"uploaded_by,omitempty"`
	UploadedAt time.Time  `gorm:"autoCreateTime" json:"uploaded_at"`
}

func (LetterOutgoingAttachment) TableName() string { return "letter_outgoing_attachments" }

type LetterOutgoingDiscussion struct {
	ID        uuid.UUID  `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	LetterID  uuid.UUID  `gorm:"type:uuid;not null" json:"letter_id"`
	ParentID  *uuid.UUID `json:"parent_id,omitempty"`
	UserID    uuid.UUID  `gorm:"not null" json:"user_id"`
	Message   string     `gorm:"not null" json:"message"`
	Mentions  JSONB      `gorm:"type:jsonb" json:"mentions,omitempty"`
	CreatedAt time.Time  `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt time.Time  `gorm:"autoUpdateTime" json:"updated_at"`
	EditedAt  *time.Time `json:"edited_at,omitempty"`

	// Relations
	User        *User                                `gorm:"foreignKey:UserID" json:"user,omitempty"`
	Attachments []LetterOutgoingDiscussionAttachment `gorm:"foreignKey:DiscussionID" json:"attachments,omitempty"`
	Replies     []LetterOutgoingDiscussion           `gorm:"foreignKey:ParentID" json:"replies,omitempty"`
}

func (LetterOutgoingDiscussion) TableName() string { return "letter_outgoing_discussions" }

type LetterOutgoingDiscussionAttachment struct {
	ID           uuid.UUID  `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	DiscussionID uuid.UUID  `gorm:"type:uuid;not null" json:"discussion_id"`
	FileURL      string     `gorm:"not null" json:"file_url"`
	FileName     string     `gorm:"not null" json:"file_name"`
	FileType     string     `gorm:"not null" json:"file_type"`
	UploadedBy   *uuid.UUID `json:"uploaded_by,omitempty"`
	UploadedAt   time.Time  `gorm:"autoCreateTime" json:"uploaded_at"`
}

func (LetterOutgoingDiscussionAttachment) TableName() string {
	return "letter_outgoing_discussion_attachments"
}