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

export type ContractPhase = typeof ContractPhase[keyof typeof ContractPhase]

export const ContractPhaseLabel: Record<number, string> = {
  1: 'Đang chọn NCC',
  2: 'Nháp',
  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',
  10: 'Đã gửi duyệt',
  98: 'Trả lại',
  99: 'Từ chối',
}

// 2026-06-16 "badge mỗi trạng thái 1 màu rõ" (anh): deepened text to -800 and
// brand-tinted the early draft phases (1/2) so they read blue not grey.
// PhaseBadge wraps these with a ring-current/15 inset for a crisp edge. Each
// pairing stays WCAG-AA on its -100 tint.
export const ContractPhaseColor: Record<number, string> = {
  1: 'bg-brand-100 text-brand-800',
  2: 'bg-slate-200 text-slate-700',
  3: 'bg-amber-100 text-amber-800',
  4: 'bg-orange-100 text-orange-800',
  5: 'bg-purple-100 text-purple-800',
  6: 'bg-indigo-100 text-indigo-800',
  7: 'bg-fuchsia-100 text-fuchsia-800',
  8: 'bg-pink-100 text-pink-800',
  9: 'bg-emerald-100 text-emerald-800',
  10: 'bg-amber-100 text-amber-800',
  98: 'bg-yellow-100 text-yellow-800',
  99: 'bg-red-100 text-red-800',
}

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
  // [W7 KHKK S161] Cờ derive 1-MỐC cho trang "Hợp đồng cứng" (GĐ4): có ≥1 attachment
  // `AttachmentPurpose.SealedCopy = 3` ⇒ badge "Đã lưu bản cứng". BE append CUỐI
  // `ContractListItemDto` (projection, không trả cả list attachments).
  hasSealedCopy: boolean
}

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[]
}

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
  // [S61 Mig 50] budgetId/budget link DROP (module Budget cũ xóa) — HĐ GIỮ
  // ngân sách nhập tay.
  budgetManualName: string | null
  budgetManualAmount: number | null
  approvals: ContractApproval[]
  comments: ContractComment[]
  attachments: ContractAttachment[]
  workflow: WorkflowSummary
  // [Plan B S29 2026-05-22 Chunk E3] V2 workflow state — mirror PE pattern.
  // Khi pin V2 (approvalWorkflowId set) → render Section 5 dynamic.
  // V1 contract: 3 fields = null.
  approvalWorkflowId: string | null
  currentApprovalLevelOrder: number | null
  levelOpinions: ContractLevelOpinion[] | null
}

// [Plan B S29 2026-05-22 Chunk E3] Mirror BE ContractLevelOpinionDto 12 fields.
// Service ApproveV2Async UPSERT (Plan B Chunk B2). Comment empty → "(duyệt —
// không ý kiến)". signedByUserId !== approverUserId → banner "Admin duyệt thay".
export type ContractLevelOpinion = {
  id: string
  approvalWorkflowLevelId: string
  stepOrder: number
  stepName: string
  levelOrder: number
  levelName: string | null
  approverUserId: string
  approverFullName: string | null
  comment: string
  signedAt: string
  signedByUserId: string
  signedByFullName: string | null
}
