package contract

import (
	"time"

	"github.com/google/uuid"
)

type CreateIncomingLetterAttachment struct {
	FileURL  string `json:"file_url"`
	FileName string `json:"file_name"`
	FileType string `json:"file_type"`
}

type CreateIncomingLetterRequest struct {
	ReferenceNumber     *string                          `json:"reference_number,omitempty"`
	Subject             string                           `json:"subject"`
	Description         *string                          `json:"description,omitempty"`
	PriorityID          *uuid.UUID                       `json:"priority_id,omitempty"`
	SenderInstitutionID *uuid.UUID                       `json:"sender_institution_id,omitempty"`
	ReceivedDate        time.Time                        `json:"received_date"`
	DueDate             *time.Time                       `json:"due_date,omitempty"`
	Attachments         []CreateIncomingLetterAttachment `json:"attachments,omitempty"`
}

type IncomingLetterAttachmentResponse struct {
	ID         uuid.UUID `json:"id"`
	FileURL    string    `json:"file_url"`
	FileName   string    `json:"file_name"`
	FileType   string    `json:"file_type"`
	UploadedAt time.Time `json:"uploaded_at"`
}

type IncomingLetterResponse struct {
	ID                uuid.UUID                          `json:"id"`
	LetterNumber      string                             `json:"letter_number"`
	ReferenceNumber   *string                            `json:"reference_number,omitempty"`
	Subject           string                             `json:"subject"`
	Description       *string                            `json:"description,omitempty"`
	Priority          *PriorityResponse                  `json:"priority,omitempty"`
	SenderInstitution *InstitutionResponse               `json:"sender_institution,omitempty"`
	ReceivedDate      time.Time                          `json:"received_date"`
	DueDate           *time.Time                         `json:"due_date,omitempty"`
	Status            string                             `json:"status"`
	CreatedBy         uuid.UUID                          `json:"created_by"`
	CreatedAt         time.Time                          `json:"created_at"`
	UpdatedAt         time.Time                          `json:"updated_at"`
	Attachments       []IncomingLetterAttachmentResponse `json:"attachments"`
}

type UpdateIncomingLetterRequest struct {
	ReferenceNumber     *string    `json:"reference_number,omitempty"`
	Subject             *string    `json:"subject,omitempty"`
	Description         *string    `json:"description,omitempty"`
	PriorityID          *uuid.UUID `json:"priority_id,omitempty"`
	SenderInstitutionID *uuid.UUID `json:"sender_institution_id,omitempty"`
	ReceivedDate        *time.Time `json:"received_date,omitempty"`
	DueDate             *time.Time `json:"due_date,omitempty"`
	Status              *string    `json:"status,omitempty"`
}

type ListIncomingLettersRequest struct {
	Page   int     `json:"page"`
	Limit  int     `json:"limit"`
	Status *string `json:"status,omitempty"`
	Query  *string `json:"query,omitempty"`
}

type ListIncomingLettersResponse struct {
	Letters    []IncomingLetterResponse `json:"letters"`
	Pagination PaginationResponse       `json:"pagination"`
}

type CreateDispositionActionSelection struct {
	ActionID uuid.UUID `json:"action_id"`
	Note     *string   `json:"note,omitempty"`
}

type CreateLetterDispositionRequest struct {
	FromDepartment  uuid.UUID                          `json:"from_department"`
	LetterID        uuid.UUID                          `json:"letter_id"`
	ToDepartmentIDs []uuid.UUID                        `json:"to_department_ids"`
	Notes           *string                            `json:"notes,omitempty"`
	SelectedActions []CreateDispositionActionSelection `json:"selected_actions,omitempty"`
}

type DispositionResponse struct {
	ID           uuid.UUID  `json:"id"`
	LetterID     uuid.UUID  `json:"letter_id"`
	DepartmentID *uuid.UUID `json:"department_id,omitempty"`
	Notes        *string    `json:"notes,omitempty"`
	ReadAt       *time.Time `json:"read_at,omitempty"`
	CreatedBy    uuid.UUID  `json:"created_by"`
	CreatedAt    time.Time  `json:"created_at"`
	UpdatedAt    time.Time  `json:"updated_at"`
}

type ListDispositionsResponse struct {
	Dispositions []DispositionResponse `json:"dispositions"`
}

type EnhancedDispositionResponse struct {
	ID               uuid.UUID                            `json:"id"`
	LetterID         uuid.UUID                            `json:"letter_id"`
	DepartmentID     *uuid.UUID                           `json:"department_id,omitempty"`
	Notes            *string                              `json:"notes,omitempty"`
	ReadAt           *time.Time                           `json:"read_at,omitempty"`
	CreatedBy        uuid.UUID                            `json:"created_by"`
	CreatedAt        time.Time                            `json:"created_at"`
	UpdatedAt        time.Time                            `json:"updated_at"`
	Department       DepartmentResponse                   `json:"department"`
	Departments      []DispositionDepartmentResponse      `json:"departments"`
	Actions          []DispositionActionSelectionResponse `json:"actions"`
	DispositionNotes []DispositionNoteResponse            `json:"disposition_notes"`
}

type DispositionDepartmentResponse struct {
	ID           uuid.UUID           `json:"id"`
	DepartmentID uuid.UUID           `json:"department_id"`
	CreatedAt    time.Time           `json:"created_at"`
	Department   *DepartmentResponse `json:"department,omitempty"`
}

type DispositionActionSelectionResponse struct {
	ID        uuid.UUID                  `json:"id"`
	ActionID  uuid.UUID                  `json:"action_id"`
	Action    *DispositionActionResponse `json:"action,omitempty"`
	Note      *string                    `json:"note,omitempty"`
	CreatedBy uuid.UUID                  `json:"created_by"`
	CreatedAt time.Time                  `json:"created_at"`
}

type DispositionNoteResponse struct {
	ID        uuid.UUID     `json:"id"`
	UserID    *uuid.UUID    `json:"user_id,omitempty"`
	Note      string        `json:"note"`
	CreatedAt time.Time     `json:"created_at"`
	User      *UserResponse `json:"user,omitempty"`
}

type ListEnhancedDispositionsResponse struct {
	Dispositions []EnhancedDispositionResponse `json:"dispositions"`
	Discussions  []LetterDiscussionResponse    `json:"discussions"`
}

type CreateLetterDiscussionRequest struct {
	ParentID *uuid.UUID             `json:"parent_id,omitempty"`
	Message  string                 `json:"message"`
	Mentions map[string]interface{} `json:"mentions,omitempty"`
}

type UpdateLetterDiscussionRequest struct {
	Message  string                 `json:"message"`
	Mentions map[string]interface{} `json:"mentions,omitempty"`
}

type LetterDiscussionResponse struct {
	ID        uuid.UUID              `json:"id"`
	LetterID  uuid.UUID              `json:"letter_id"`
	ParentID  *uuid.UUID             `json:"parent_id,omitempty"`
	UserID    uuid.UUID              `json:"user_id"`
	Message   string                 `json:"message"`
	Mentions  map[string]interface{} `json:"mentions,omitempty"`
	CreatedAt time.Time              `json:"created_at"`
	UpdatedAt time.Time              `json:"updated_at"`
	EditedAt  *time.Time             `json:"edited_at,omitempty"`

	// Preloaded user profile who created the discussion
	User *UserResponse `json:"user,omitempty"`

	// Preloaded user profiles for mentions
	MentionedUsers []UserResponse `json:"mentioned_users,omitempty"`
}
