import type {
  ReportStatus,
  StageType,
  FindingStatus,
  GroupKey,
} from '../types/audit-report-builder.types';

export const REPORT_STATUS_CONFIG: Record<
  ReportStatus,
  { label: string; bg: string; text: string; dot: string }
> = {
  draft: { label: 'Draft', bg: '#e5e7eb', text: '#374151', dot: '#6b7280' },
  submitted: { label: 'Submitted', bg: '#dbeafe', text: '#1e40af', dot: '#3b82f6' },
  returned: { label: 'Returned', bg: '#fef3c7', text: '#92400e', dot: '#f59e0b' },
  approved: { label: 'Approved', bg: '#dcfce7', text: '#166534', dot: '#16a34a' },
  locked: { label: 'Locked', bg: '#ede9fe', text: '#5b21b6', dot: '#7c3aed' },
};

export const STAGE_CONFIG: Record<StageType, { label: string; bg: string; color: string }> = {
  stage1: { label: 'Stage 1', bg: '#dbeafe', color: '#1e40af' },
  stage2: { label: 'Stage 2', bg: '#ede9fe', color: '#5b21b6' },
  surveillance: { label: 'Surveillance', bg: '#ccfbf1', color: '#0f766e' },
  recert: { label: 'Re-Certification', bg: '#ffedd5', color: '#9a3412' },
};

export const FINDING_OPTIONS: {
  value: FindingStatus;
  label: string;
  bg: string;
  color: string;
  border: string;
}[] = [
  { value: 'C', label: 'C', bg: '#dcfce7', color: '#166534', border: '#86efac' },
  { value: 'NC', label: 'NC', bg: '#fee2e2', color: '#991b1b', border: '#fca5a5' },
  { value: 'OFI', label: 'OFI', bg: '#fef3c7', color: '#92400e', border: '#fcd34d' },
  { value: 'NA', label: 'NA', bg: '#e5e7eb', color: '#374151', border: '#cbd5e1' },
];

export const GROUP_LABEL: Record<GroupKey, string> = {
  clause: 'Clause Review',
  performance: 'Performance Evaluation',
  risk: 'Risk & Opportunities',
  feedback: 'Customer Feedback',
  statutory: 'Statutory & Regulatory',
};

/** Header fields shown in the editor. Keys must match backend EDITABLE_FIELDS. */
export const HEADER_FIELD_META: Record<string, { label: string; multiline?: boolean; wide?: boolean }> = {
  name: { label: 'Organization Name', wide: true },
  address: { label: 'Address', multiline: true, wide: true },
  site_address: { label: 'Site Address', multiline: true, wide: true },
  contact_person: { label: 'Contact Person' },
  telephone: { label: 'Telephone / Mobile' },
  email: { label: 'E-mail' },
  emails: { label: 'Other E-mails' },
  employees: { label: 'No. of Employees' },
  multisite: { label: 'Multi-site' },
  scope: { label: 'Scope of Certification', multiline: true, wide: true },
  cert_no: { label: 'Certificate No.' },
  ea_code: { label: 'NACE / EA Code' },
  accreditation: { label: 'Accreditation' },
  lead_auditor: { label: 'Lead Auditor' },
  auditors: { label: 'Auditor(s)' },
  technical_expert: { label: 'Technical Expert' },
  man_days: { label: 'Man-days' },
  audit_date: { label: 'Audit Date' },
};

export function fmtDate(v?: string | null): string {
  if (!v) return '—';
  const d = new Date(String(v).length === 10 ? `${v}T00:00:00` : v);
  if (isNaN(d.getTime())) return String(v);
  return d.toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric' });
}
