// Types cho module Duyệt NCC (PurchaseEvaluation) — mirror BE Domain.

export const PurchaseEvaluationType = {
  DuyetNcc: 1,
  DuyetNccPhuongAn: 2,
} as const
export type PurchaseEvaluationType = typeof PurchaseEvaluationType[keyof typeof PurchaseEvaluationType]

export const PurchaseEvaluationTypeLabel: Record<number, string> = {
  1: 'Duyệt NCC',
  2: 'Duyệt NCC và Giải pháp',
}

export const PurchaseEvaluationTypeCode: Record<number, string> = {
  1: 'DuyetNcc',
  2: 'DuyetNccPhuongAn',
}

export const PurchaseEvaluationPhase = {
  DangSoanThao: 1,
  ChoPurchasing: 2,
  ChoDuAn: 3,
  ChoCCM: 4,
  ChoCEODuyetPA: 5,
  ChoCEODuyetNCC: 6,
  DaDuyet: 7,
  TuChoi: 99,
} as const
export type PurchaseEvaluationPhase = typeof PurchaseEvaluationPhase[keyof typeof PurchaseEvaluationPhase]

export const PurchaseEvaluationPhaseLabel: Record<number, string> = {
  1: 'Đang soạn thảo',
  2: 'Chờ Purchasing',
  3: 'Chờ Dự án',
  4: 'Chờ CCM',
  5: 'Chờ CEO duyệt PA',
  6: 'Chờ CEO duyệt NCC',
  7: 'Đã duyệt',
  99: 'Từ chối',
}

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

export type PeListItem = {
  id: string
  maPhieu: string | null
  tenGoiThau: string
  type: number
  phase: number
  projectId: string
  projectName: string
  selectedSupplierId: string | null
  selectedSupplierName: string | null
  contractId: string | null
  slaDeadline: string | null
  createdAt: string
}

export type PeSupplier = {
  id: string
  supplierId: string
  supplierName: string
  displayName: string | null
  contactName: string | null
  contactEmail: string | null
  contactPhone: string | null
  paymentTermText: string | null
  note: string | null
  order: number
}

export type PeQuote = {
  id: string
  purchaseEvaluationDetailId: string
  purchaseEvaluationSupplierId: string
  bgVat: number
  chuaVat: number
  thanhTien: number
  isSelected: boolean
  note: string | null
}

export type PeDetailRow = {
  id: string
  groupCode: string
  groupName: string
  itemCode: string | null
  noiDung: string
  donViTinh: string | null
  khoiLuongNganSach: number
  khoiLuongThiCong: number
  donGiaNganSach: number
  thanhTienNganSach: number
  order: number
  ghiChu: string | null
  quotes: PeQuote[]
}

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

export const PeAttachmentPurpose = {
  QuoteDocument: 1,
  RequirementSpec: 2,
  DecisionExport: 3,
  ComparisonTable: 4,
  Other: 99,
} as const

export const PeAttachmentPurposeLabel: Record<number, string> = {
  1: 'Báo giá',
  2: 'Yêu cầu KT',
  3: 'Phiếu duyệt',
  4: 'Bảng so sánh',
  99: 'Khác',
}

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

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

export type PeChangelog = {
  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
}

// Snapshot ngân sách link (compact — cùng shape BudgetSummaryDto BE).
export type BudgetSummary = {
  id: string
  maNganSach: string | null
  tenNganSach: string
  namNganSach: number
  phase: number
  tongNganSach: number
}

// Mirror BE PeDepartmentKind enum
export const PeDepartmentKind = {
  PheDuyet: 1,
  Ccm: 2,
  MuaHang: 3,
  SmPm: 4,
} as const
export type PeDepartmentKind = typeof PeDepartmentKind[keyof typeof PeDepartmentKind]

export const PeDepartmentKindLabel: Record<number, string> = {
  1: 'Phê duyệt',
  2: 'P.CCM',
  3: 'P.Mua hàng',
  4: 'SM-PM',
}

export type PeDepartmentOpinion = {
  id: string
  kind: number
  kindLabel: string
  opinion: string | null
  signedAt: string | null
  userId: string | null
  userName: string | null
}

// 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 (PE, 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 const ApprovalStageLabel: Record<number, string> = {
  1: 'Review NV',
  2: 'Confirm TPB',
}

export type PeDepartmentApproval = {
  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 PeDetailBundle = {
  id: string
  maPhieu: string | null
  type: number
  phase: number
  tenGoiThau: string
  diaDiem: string | null
  moTa: string | null
  projectId: string
  projectName: string
  departmentId: string | null
  departmentName: string | null
  drafterUserId: string | null
  drafterName: string | null
  selectedSupplierId: string | null
  selectedSupplierName: string | null
  contractId: string | null
  paymentTerms: string | null
  slaDeadline: string | null
  createdAt: string
  updatedAt: string | null
  budgetId: string | null
  budget: BudgetSummary | null
  // Mig 17 — manual budget fallback khi không link Budget entity. Cả 2 cùng null OK.
  budgetManualName: string | null
  budgetManualAmount: number | null
  suppliers: PeSupplier[]
  details: PeDetailRow[]
  approvals: PeApproval[]
  attachments: PeAttachment[]
  departmentOpinions: PeDepartmentOpinion[]
  workflow: PeWorkflowSummary
}
