export const ContractPhase = {
  DangChon: 1,
  DangSoanThao: 2,
  DangGopY: 3,
  DangDamPhan: 4,
  DangInKy: 5,
  DangKiemTraCCM: 6,
  DangTrinhKy: 7,
  DangDongDau: 8,
  DaPhatHanh: 9,
  TuChoi: 99,
} as const

export type ContractPhase = typeof ContractPhase[keyof typeof ContractPhase]

export const ContractPhaseLabel: Record<number, string> = {
  1: 'Đang chọn NCC',
  2: 'Đang soạn thảo',
  3: 'Đang góp ý',
  4: 'Đang đàm phán',
  5: 'Đang in ký',
  6: 'CCM kiểm tra',
  7: 'Đang trình ký',
  8: 'Đang đóng dấu',
  9: 'Đã phát hành',
  99: 'Từ chối',
}

export const ContractPhaseColor: Record<number, string> = {
  1: 'bg-slate-100 text-slate-700',
  2: 'bg-blue-100 text-blue-700',
  3: 'bg-amber-100 text-amber-700',
  4: 'bg-orange-100 text-orange-700',
  5: 'bg-purple-100 text-purple-700',
  6: 'bg-indigo-100 text-indigo-700',
  7: 'bg-fuchsia-100 text-fuchsia-700',
  8: 'bg-pink-100 text-pink-700',
  9: 'bg-emerald-100 text-emerald-700',
  99: 'bg-red-100 text-red-700',
}

export const ApprovalDecision = {
  Pending: 0,
  Approve: 1,
  Reject: 2,
  AutoApprove: 3,
} as const

export type ApprovalDecision = typeof ApprovalDecision[keyof typeof ApprovalDecision]

// 2-stage department approval (Migration 16) — Stage 1=Review NV, 2=Confirm TPB.
// BLOCK transition khi NV review chưa có TPB confirm cùng (HĐ, Phase, Dept).
// CanBypassReview=true → NV được Stage=Confirm + IsBypassed=true (skip Review).
export const ApprovalStage = {
  Review: 1,
  Confirm: 2,
} as const
export type ApprovalStage = typeof ApprovalStage[keyof typeof ApprovalStage]

export type ContractDepartmentApproval = {
  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 ContractListItem = {
  id: string
  maHopDong: string | null
  tenHopDong: string | null
  type: number
  phase: number
  supplierId: string
  supplierName: string
  projectId: string
  projectName: string
  giaTri: number
  slaDeadline: string | null
  createdAt: string
}

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

export type ContractComment = {
  id: string
  userId: string
  userName: string
  phase: number
  content: string
  createdAt: string
}

export type ContractAttachment = {
  id: string
  fileName: string
  storagePath: string
  fileSize: number
  contentType: string
  purpose: number
  note: string | null
  createdAt: string
}

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

// Snapshot ngân sách link cho Contract (cùng shape với BudgetSummaryDto BE).
export type ContractBudgetSummary = {
  id: string
  maNganSach: string | null
  tenNganSach: string
  namNganSach: number
  phase: number
  tongNganSach: number
}

export type ContractDetail = {
  id: string
  maHopDong: string | null
  tenHopDong: string | null
  noiDung: string | null
  type: number
  phase: number
  supplierId: string
  supplierName: string
  projectId: string
  projectName: string
  departmentId: string | null
  departmentName: string | null
  drafterUserId: string | null
  drafterName: string | null
  templateId: string | null
  giaTri: number
  bypassProcurementAndCCM: boolean
  slaDeadline: string | null
  draftData: string | null
  createdAt: string
  updatedAt: string | null
  budgetId: string | null
  budget: ContractBudgetSummary | null
  // Mig 17 — manual budget fallback khi không link Budget entity.
  budgetManualName: string | null
  budgetManualAmount: number | null
  approvals: ContractApproval[]
  comments: ContractComment[]
  attachments: ContractAttachment[]
  workflow: WorkflowSummary
}
