package entities

import (
	"time"

	"github.com/google/uuid"
)

type ApprovalFlow struct {
	ID           uuid.UUID            `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	DepartmentID uuid.UUID            `gorm:"type:uuid;not null" json:"department_id"`
	Name         string               `gorm:"not null" json:"name"`
	Description  *string              `json:"description,omitempty"`
	IsActive     bool                 `gorm:"default:true" json:"is_active"`
	CreatedAt    time.Time            `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt    time.Time            `gorm:"autoUpdateTime" json:"updated_at"`

	// Relations
	Department *Department          `gorm:"foreignKey:DepartmentID" json:"department,omitempty"`
	Steps      []ApprovalFlowStep   `gorm:"foreignKey:FlowID" json:"steps,omitempty"`
}

func (ApprovalFlow) TableName() string { return "approval_flows" }

type ApprovalFlowStep struct {
	ID             uuid.UUID  `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	FlowID         uuid.UUID  `gorm:"type:uuid;not null" json:"flow_id"`
	StepOrder      int        `gorm:"not null" json:"step_order"`
	ParallelGroup  int        `gorm:"default:1" json:"parallel_group"`
	ApproverRoleID *uuid.UUID `json:"approver_role_id,omitempty"`
	ApproverUserID *uuid.UUID `json:"approver_user_id,omitempty"`
	Required       bool       `gorm:"default:true" json:"required"`
	CreatedAt      time.Time  `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt      time.Time  `gorm:"autoUpdateTime" json:"updated_at"`

	// Relations
	ApprovalFlow *ApprovalFlow `gorm:"foreignKey:FlowID" json:"approval_flow,omitempty"`
	ApproverRole *Role         `gorm:"foreignKey:ApproverRoleID" json:"approver_role,omitempty"`
	ApproverUser *User         `gorm:"foreignKey:ApproverUserID" json:"approver_user,omitempty"`
}

func (ApprovalFlowStep) TableName() string { return "approval_flow_steps" }

type ApprovalStatus string

const (
	ApprovalStatusPending  ApprovalStatus = "pending"
	ApprovalStatusApproved ApprovalStatus = "approved"
	ApprovalStatusRejected ApprovalStatus = "rejected"
)

type LetterOutgoingApproval 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"`
	StepID     uuid.UUID      `gorm:"type:uuid;not null" json:"step_id"`
	ApproverID *uuid.UUID     `json:"approver_id,omitempty"`
	Status     ApprovalStatus `gorm:"not null;default:'pending'" json:"status"`
	Remarks    *string        `json:"remarks,omitempty"`
	ActedAt    *time.Time     `json:"acted_at,omitempty"`
	CreatedAt  time.Time      `gorm:"autoCreateTime" json:"created_at"`

	// Relations
	Letter   *LetterOutgoing   `gorm:"foreignKey:LetterID" json:"letter,omitempty"`
	Step     *ApprovalFlowStep `gorm:"foreignKey:StepID" json:"step,omitempty"`
	Approver *User             `gorm:"foreignKey:ApproverID" json:"approver,omitempty"`
}

func (LetterOutgoingApproval) TableName() string { return "letter_outgoing_approvals" }