package contract

import (
	"time"

	"github.com/google/uuid"
)

type CreateOutgoingLetterRecipient struct {
	LetterID     uuid.UUID  `json:"letter_id"`
	UserID       *uuid.UUID `json:"user_id,omitempty"`
	DepartmentID *uuid.UUID `json:"department_id,omitempty"`
	IsPrimary    bool       `json:"is_primary"`
	Status       string     `json:"status"`
	Flag         *string    `json:"flag,omitempty"`
	IsArchived   bool       `json:"is_archived"`
}

type CreateOutgoingLetterAttachment struct {
	FileURL  string `json:"file_url" validate:"required"`
	FileName string `json:"file_name" validate:"required"`
	FileType string `json:"file_type" validate:"required"`
}

type CreateOutgoingLetterRequest struct {
	ReferenceNumber       *string                          `json:"reference_number,omitempty"`
	Subject               string                           `json:"subject" validate:"required"`
	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" validate:"required"`
	Attachments           []CreateOutgoingLetterAttachment `json:"attachments,omitempty"`
	UserID                uuid.UUID
}

type OutgoingLetterRecipientResponse struct {
	ID           uuid.UUID           `json:"id"`
	LetterID     uuid.UUID           `json:"letter_id"`
	UserID       *uuid.UUID          `json:"user_id,omitempty"`
	DepartmentID *uuid.UUID          `json:"department_id,omitempty"`
	IsPrimary    bool                `json:"is_primary"`
	Status       string              `json:"status"`
	ReadAt       *time.Time          `json:"read_at,omitempty"`
	Flag         *string             `json:"flag,omitempty"`
	IsArchived   bool                `json:"is_archived"`
	CreatedAt    time.Time           `json:"created_at"`
	User         *UserResponse       `json:"user,omitempty"`
	Department   *DepartmentResponse `json:"department,omitempty"`
}

type OutgoingLetterAttachmentResponse 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 OutgoingLetterApprovalResponse struct {
	ID            uuid.UUID  `json:"id"`
	StepOrder     int        `json:"step_order"`
	ParallelGroup int        `json:"parallel_group"`
	IsRequired    bool       `json:"is_required"`
	ApproverID    *uuid.UUID `json:"approver_id,omitempty"`
	Status        string     `json:"status"`
	Remarks       *string    `json:"remarks,omitempty"`
	ActedAt       *time.Time `json:"acted_at,omitempty"`
	CreatedAt     time.Time  `json:"created_at"`
}

type OutgoingLetterResponse 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"`
	PriorityID            *uuid.UUID                         `json:"priority_id,omitempty"`
	Priority              *PriorityResponse                  `json:"priority,omitempty"`
	ReceiverInstitutionID *uuid.UUID                         `json:"receiver_institution_id,omitempty"`
	ReceiverInstitution   *InstitutionResponse               `json:"receiver_institution,omitempty"`
	IssueDate             time.Time                          `json:"issue_date"`
	Status                string                             `json:"status"`
	ApprovalFlowID        *uuid.UUID                         `json:"approval_flow_id,omitempty"`
	CreatedBy             uuid.UUID                          `json:"created_by"`
	CreatedAt             time.Time                          `json:"created_at"`
	UpdatedAt             time.Time                          `json:"updated_at"`
	Recipients            []OutgoingLetterRecipientResponse  `json:"recipients,omitempty"`
	Attachments           []OutgoingLetterAttachmentResponse `json:"attachments,omitempty"`
	Approvals             []OutgoingLetterApprovalResponse   `json:"approvals,omitempty"`
}

type UpdateOutgoingLetterRequest struct {
	ReferenceNumber       *string    `json:"reference_number,omitempty"`
	Subject               *string    `json:"subject,omitempty"`
	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,omitempty"`
}

type ListOutgoingLettersRequest struct {
	Page                  int        `form:"page" json:"page"`
	Limit                 int        `form:"limit" json:"limit"`
	Status                string     `form:"status" json:"status,omitempty"`
	Query                 string     `form:"q" json:"query,omitempty"`
	CreatedBy             *uuid.UUID `form:"created_by" json:"created_by,omitempty"`
	DepartmentID          *uuid.UUID `form:"department_id" json:"department_id,omitempty"`
	ReceiverInstitutionID *uuid.UUID `form:"receiver_institution_id" json:"receiver_institution_id,omitempty"`
	FromDate              string     `form:"from_date" json:"from_date,omitempty"`
	ToDate                string     `form:"to_date" json:"to_date,omitempty"`
	PriorityID            *uuid.UUID `form:"priority_id" json:"priority_id,omitempty"`
	SortBy                string     `form:"sort_by" json:"sort_by,omitempty"`
	SortOrder             string     `form:"sort_order" json:"sort_order,omitempty"`
	IsArchived            *bool      `form:"is_archived" json:"is_archived,omitempty"`
}

type ListOutgoingLettersResponse struct {
	Items []*OutgoingLetterResponse `json:"items"`
	Total int64                     `json:"total"`
}

type ApproveLetterRequest struct {
	Remarks *string `json:"remarks,omitempty"`
}

type RejectLetterRequest struct {
	Reason string `json:"reason" validate:"required"`
}

type AddRecipientsRequest struct {
	Recipients []CreateOutgoingLetterRecipient `json:"recipients" validate:"required,dive"`
}

type UpdateRecipientRequest struct {
	UserID       *uuid.UUID `json:"user_id,omitempty"`
	DepartmentID *uuid.UUID `json:"department_id,omitempty"`
	IsPrimary    bool       `json:"is_primary"`
	Status       *string    `json:"status,omitempty"`
	Flag         *string    `json:"flag,omitempty"`
	IsArchived   *bool      `json:"is_archived,omitempty"`
}

type AddAttachmentsRequest struct {
	Attachments []CreateOutgoingLetterAttachment `json:"attachments" validate:"required,dive"`
}

type CreateDiscussionAttachment struct {
	FileURL  string `json:"file_url" validate:"required"`
	FileName string `json:"file_name" validate:"required"`
	FileType string `json:"file_type" validate:"required"`
}

type CreateDiscussionRequest struct {
	ParentID    *uuid.UUID                   `json:"parent_id,omitempty"`
	Message     string                       `json:"message" validate:"required"`
	Mentions    map[string]interface{}       `json:"mentions,omitempty"`
	Attachments []CreateDiscussionAttachment `json:"attachments,omitempty"`
}

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

type DiscussionResponse struct {
	ID        uuid.UUID              `json:"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"`
}

type ApprovalFlowRequest struct {
	DepartmentID uuid.UUID                 `json:"department_id" validate:"required"`
	Name         string                    `json:"name" validate:"required"`
	Description  *string                   `json:"description,omitempty"`
	IsActive     bool                      `json:"is_active"`
	Steps        []ApprovalFlowStepRequest `json:"steps" validate:"required,dive"`
}

type ApprovalFlowStepRequest struct {
	StepOrder      int        `json:"step_order" validate:"required,min=1"`
	ParallelGroup  int        `json:"parallel_group" validate:"min=1"`
	ApproverRoleID *uuid.UUID `json:"approver_role_id,omitempty"`
	ApproverUserID *uuid.UUID `json:"approver_user_id,omitempty"`
	Required       bool       `json:"required"`
}

type ApprovalFlowResponse struct {
	ID           uuid.UUID                  `json:"id"`
	DepartmentID uuid.UUID                  `json:"department_id"`
	Department   *DepartmentResponse        `json:"department,omitempty"`
	Name         string                     `json:"name"`
	Description  *string                    `json:"description,omitempty"`
	IsActive     bool                       `json:"is_active"`
	Steps        []ApprovalFlowStepResponse `json:"steps,omitempty"`
	CreatedAt    time.Time                  `json:"created_at"`
	UpdatedAt    time.Time                  `json:"updated_at"`
}

type ApprovalFlowStepResponse struct {
	ID             uuid.UUID     `json:"id"`
	StepOrder      int           `json:"step_order"`
	ParallelGroup  int           `json:"parallel_group"`
	ApproverRoleID *uuid.UUID    `json:"approver_role_id,omitempty"`
	ApproverRole   *RoleResponse `json:"approver_role,omitempty"`
	ApproverUserID *uuid.UUID    `json:"approver_user_id,omitempty"`
	ApproverUser   *UserResponse `json:"approver_user,omitempty"`
	Required       bool          `json:"required"`
	CreatedAt      time.Time     `json:"created_at"`
	UpdatedAt      time.Time     `json:"updated_at"`
}

type ListApprovalFlowsRequest struct {
	Limit        int        `json:"limit"`
	Offset       int        `json:"offset"`
	DepartmentID *uuid.UUID `json:"department_id,omitempty"`
	IsActive     *bool      `json:"is_active,omitempty"`
}

type ListApprovalFlowsResponse struct {
	Items []*ApprovalFlowResponse `json:"items"`
	Total int64                   `json:"total"`
}

// Letter Approval Information for Approver
type LetterApprovalInfoResponse struct {
	IsApproverOnActiveStep bool             `json:"is_approver_on_active_step"`
	DecisionStatus         string           `json:"decision_status"`
	CanApprove             bool             `json:"can_approve"`
	Actions                []ApprovalAction `json:"actions"`
	NotesVisibility        string           `json:"notes_visibility"`
}

type ApprovalAction struct {
	Type   string `json:"type"`
	Href   string `json:"href"`
	Method string `json:"method"`
}

// OutgoingLetterApprovalDiscussionsResponse combines approvals and discussions for outgoing letters
type OutgoingLetterApprovalDiscussionsResponse struct {
	Approvals   []EnhancedOutgoingLetterApprovalResponse `json:"approvals"`
	Discussions []OutgoingLetterDiscussionResponse       `json:"discussions"`
}

// EnhancedOutgoingLetterApprovalResponse includes approval details with related data
type EnhancedOutgoingLetterApprovalResponse struct {
	ID            uuid.UUID                 `json:"id"`
	LetterID      uuid.UUID                 `json:"letter_id"`
	StepID        uuid.UUID                 `json:"step_id"`
	StepOrder     int                       `json:"step_order"`
	ParallelGroup int                       `json:"parallel_group"`
	IsRequired    bool                      `json:"is_required"`
	ApproverID    *uuid.UUID                `json:"approver_id,omitempty"`
	Status        string                    `json:"status"`
	Remarks       *string                   `json:"remarks,omitempty"`
	ActedAt       *time.Time                `json:"acted_at,omitempty"`
	CreatedAt     time.Time                 `json:"created_at"`
	Step          *ApprovalFlowStepResponse `json:"step,omitempty"`
	Approver      *UserResponse             `json:"approver,omitempty"`
}

// GetLetterApprovalsResponse represents the list of approvals for a letter
type GetLetterApprovalsResponse struct {
	LetterID     uuid.UUID                                `json:"letter_id"`
	LetterNumber string                                   `json:"letter_number"`
	LetterStatus string                                   `json:"letter_status"`
	TotalSteps   int                                      `json:"total_steps"`
	CurrentStep  int                                      `json:"current_step"`
	Approvals    []EnhancedOutgoingLetterApprovalResponse `json:"approvals"`
}

// OutgoingLetterDiscussionResponse represents a discussion on an outgoing letter
type OutgoingLetterDiscussionResponse 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"`
	User           *UserResponse                                `json:"user,omitempty"`
	MentionedUsers []UserResponse                               `json:"mentioned_users,omitempty"`
	Attachments    []OutgoingLetterDiscussionAttachmentResponse `json:"attachments,omitempty"`
}

// OutgoingLetterDiscussionAttachmentResponse represents an attachment in a discussion
type OutgoingLetterDiscussionAttachmentResponse struct {
	ID           uuid.UUID  `json:"id"`
	DiscussionID uuid.UUID  `json:"discussion_id"`
	FileURL      string     `json:"file_url"`
	FileName     string     `json:"file_name"`
	FileType     string     `json:"file_type"`
	UploadedBy   *uuid.UUID `json:"uploaded_by,omitempty"`
	UploadedAt   time.Time  `json:"uploaded_at"`
}

// TimelineEvent represents a single event in the approval timeline
type TimelineEvent struct {
	ID          string                               `json:"id"`
	Type        string                               `json:"type"` // "approval", "discussion", "submission", "rejection"
	Timestamp   time.Time                            `json:"timestamp"`
	Actor       *UserResponse                        `json:"actor,omitempty"`
	Action      string                               `json:"action"`
	Description string                               `json:"description"`
	Status      string                               `json:"status,omitempty"`
	StepOrder   int                                  `json:"step_order,omitempty"`
	Message     string                               `json:"message,omitempty"`
	Data        interface{}                          `json:"data,omitempty"`
}

// ApprovalTimelineResponse represents the complete timeline for a letter
type ApprovalTimelineResponse struct {
	LetterID     uuid.UUID       `json:"letter_id"`
	LetterNumber string          `json:"letter_number"`
	Subject      string          `json:"subject"`
	Status       string          `json:"status"`
	CreatedAt    time.Time       `json:"created_at"`
	Timeline     []TimelineEvent `json:"timeline"`
	Summary      TimelineSummary `json:"summary"`
}

// TimelineSummary provides overview statistics for the timeline
type TimelineSummary struct {
	TotalSteps      int           `json:"total_steps"`
	CompletedSteps  int           `json:"completed_steps"`
	PendingSteps    int           `json:"pending_steps"`
	CurrentStep     int           `json:"current_step"`
	TotalDuration   string        `json:"total_duration"`
	AverageStepTime string        `json:"average_step_time"`
	Status          string        `json:"status"`
}
