package contract

import (
	"time"

	"github.com/google/uuid"
)

type CreateOutgoingLetterRecipient struct {
	Name        string  `json:"name" validate:"required"`
	Email       *string `json:"email,omitempty"`
	Position    *string `json:"position,omitempty"`
	Institution *string `json:"institution,omitempty"`
	IsPrimary   bool    `json:"is_primary"`
}

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"`
	ApprovalFlowID        *uuid.UUID                       `json:"approval_flow_id,omitempty"`
	Recipients            []CreateOutgoingLetterRecipient  `json:"recipients,omitempty"`
	Attachments           []CreateOutgoingLetterAttachment `json:"attachments,omitempty"`
	UserID                uuid.UUID
}

type OutgoingLetterRecipientResponse struct {
	ID          uuid.UUID `json:"id"`
	Name        string    `json:"name"`
	Email       *string   `json:"email,omitempty"`
	Position    *string   `json:"position,omitempty"`
	Institution *string   `json:"institution,omitempty"`
	IsPrimary   bool      `json:"is_primary"`
	CreatedAt   time.Time `json:"created_at"`
}

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"`
	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 {
	Limit                 int        `json:"limit"`
	Offset                int        `json:"offset"`
	Status                *string    `json:"status,omitempty"`
	Query                 *string    `json:"query,omitempty"`
	CreatedBy             *uuid.UUID `json:"created_by,omitempty"`
	ReceiverInstitutionID *uuid.UUID `json:"receiver_institution_id,omitempty"`
	FromDate              *time.Time `json:"from_date,omitempty"`
	ToDate                *time.Time `json:"to_date,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 {
	Name        string  `json:"name" validate:"required"`
	Email       *string `json:"email,omitempty"`
	Position    *string `json:"position,omitempty"`
	Institution *string `json:"institution,omitempty"`
	IsPrimary   bool    `json:"is_primary"`
}

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"`
}
