// Types cho module Ngân sách (Budget) — mirror BE Domain.Budgets.
// Workflow simple 3-step (BudgetPolicy.Default hardcoded):
// DangSoanThao → ChoCCM → ChoCEO → DaDuyet  (+ TuChoi từ DangSoanThao).

export const BudgetPhase = {
  DangSoanThao: 1,
  ChoCCM: 2,
  ChoCEO: 3,
  DaDuyet: 4,
  TuChoi: 99,
} as const
export type BudgetPhase = typeof BudgetPhase[keyof typeof BudgetPhase]

export const BudgetPhaseLabel: Record<number, string> = {
  1: 'Đang soạn thảo',
  2: 'Chờ CCM',
  3: 'Chờ CEO',
  4: 'Đã duyệt',
  99: 'Từ chối',
}

export const BudgetPhaseColor: Record<number, string> = {
  1: 'bg-slate-100 text-slate-700',
  2: 'bg-indigo-100 text-indigo-700',
  3: 'bg-pink-100 text-pink-700',
  4: 'bg-emerald-100 text-emerald-700',
  99: 'bg-red-100 text-red-700',
}

// Mirror BE BudgetEntityType enum
export const BudgetEntityType = {
  Header: 1,
  Detail: 2,
  Workflow: 3,
} as const

// Mirror BE ChangelogAction enum (reuse từ Contracts.ChangelogAction)
export const ChangelogAction = {
  Insert: 1,
  Update: 2,
  Delete: 3,
  Transition: 4,
} as const

// Mirror BE ApprovalDecision enum (reuse từ Contracts.ApprovalDecision)
export const ApprovalDecision = {
  Approve: 1,
  Reject: 2,
  AutoApprove: 3,
} as const

// 2-stage department approval (Migration 16) — Stage 1=Review NV, 2=Confirm TPB.
export const ApprovalStage = {
  Review: 1,
  Confirm: 2,
} as const
export type ApprovalStage = typeof ApprovalStage[keyof typeof ApprovalStage]

export type BudgetDepartmentApproval = {
  id: string
  phaseAtApproval: number
  departmentId: string
  departmentName: string | null
  stage: number              // 1=Review, 2=Confirm
  approverUserId: string
  approverName: string | null
  approverRoleSnapshot: string | null  // "TPB" | "NV" | "NV(bypass)"
  comment: string | null
  approvedAt: string
  isBypassed: boolean
}

export type BudgetListItem = {
  id: string
  maNganSach: string | null
  tenNganSach: string
  namNganSach: number
  phase: number
  projectId: string
  projectName: string
  tongNganSach: number
  slaDeadline: string | null
  createdAt: string
}

export type BudgetDetailRow = {
  id: string
  groupCode: string
  groupName: string
  itemCode: string | null
  noiDung: string
  donViTinh: string | null
  khoiLuong: number
  donGia: number
  thanhTien: number
  order: number
  ghiChu: string | null
}

export type BudgetApproval = {
  id: string
  fromPhase: number
  toPhase: number
  approverUserId: string | null
  approverName: string | null
  decision: number
  comment: string | null
  approvedAt: string
}

export type BudgetWorkflowSummary = {
  policyName: string
  policyDescription: string
  activePhases: number[]
  nextPhases: number[]
}

export type BudgetChangelog = {
  id: string
  entityType: number
  entityId: string | null
  action: number
  phaseAtChange: number | null
  userId: string | null
  userName: string | null
  summary: string | null
  fieldChangesJson: string | null
  contextNote: string | null
  createdAt: string
}

export type BudgetDetailBundle = {
  id: string
  maNganSach: string | null
  tenNganSach: string
  description: string | null
  namNganSach: number
  phase: number
  projectId: string
  projectName: string
  departmentId: string | null
  departmentName: string | null
  drafterUserId: string | null
  drafterName: string | null
  tongNganSach: number
  slaDeadline: string | null
  createdAt: string
  updatedAt: string | null
  details: BudgetDetailRow[]
  approvals: BudgetApproval[]
  workflow: BudgetWorkflowSummary
}

// Body shape POST/PUT detail — mirror BudgetDetailBody record BE.
export type BudgetDetailBody = {
  groupCode: string
  groupName: string
  itemCode: string | null
  noiDung: string
  donViTinh: string | null
  khoiLuong: number
  donGia: number
  thanhTien: number
  ghiChu: string | null
}
