/** Phased customs uploads stored by raw file `code` on contract/booking resources. */
export type ClearanceWorkflowFileOwner = "customer" | "gl_et" | "gl_dj";

export type ClearanceWorkflowFileCategory =
  | "declaration"
  | "duty"
  | "transit"
  | "djibouti";

export interface ClearanceWorkflowFileCatalogEntry {
  code: string;
  label: string;
  uploadedBy: ClearanceWorkflowFileOwner;
  category: ClearanceWorkflowFileCategory;
  /** Omit when the file applies to both directions. */
  tradeDirection?: "IMPORT" | "EXPORT";
}

export const CLEARANCE_WORKFLOW_FILE_CATALOG: ClearanceWorkflowFileCatalogEntry[] = [
  { code: "im4", label: "IM4 Declaration", uploadedBy: "gl_et", category: "declaration", tradeDirection: "IMPORT" },
  { code: "im5", label: "IM5 Declaration", uploadedBy: "gl_et", category: "declaration", tradeDirection: "IMPORT" },
  { code: "ex3", label: "EX3 Declaration", uploadedBy: "gl_et", category: "declaration", tradeDirection: "EXPORT" },
  { code: "ex8", label: "EX8 Declaration", uploadedBy: "gl_et", category: "declaration", tradeDirection: "EXPORT" },
  { code: "duty_tax_notice", label: "Duty / Tax Notice", uploadedBy: "gl_et", category: "duty" },
  { code: "duty_tax_receipt", label: "Duty / Tax Payment Slip", uploadedBy: "customer", category: "duty" },
  {
    code: "duty_tax_notice_2",
    label: "Additional Duty / Tax Notice",
    uploadedBy: "gl_et",
    category: "duty",
    tradeDirection: "IMPORT",
  },
  {
    code: "duty_tax_receipt_2",
    label: "Additional Duty / Tax Payment Slip",
    uploadedBy: "customer",
    category: "duty",
    tradeDirection: "IMPORT",
  },
  {
    code: "import_release",
    label: "Import Release",
    uploadedBy: "gl_et",
    category: "declaration",
    tradeDirection: "IMPORT",
  },
  { code: "transit_permitted", label: "Transit Permit", uploadedBy: "gl_et", category: "transit", tradeDirection: "IMPORT" },
  {
    code: "export_transport_document",
    label: "Transit Permit",
    uploadedBy: "gl_et",
    category: "transit",
    tradeDirection: "EXPORT",
  },
  { code: "delivery_order", label: "Delivery Order", uploadedBy: "gl_dj", category: "djibouti", tradeDirection: "IMPORT" },
  { code: "release_order", label: "Release Order", uploadedBy: "gl_dj", category: "djibouti", tradeDirection: "EXPORT" },
  {
    code: "t1_transport_document",
    label: "T1 Transport Document",
    uploadedBy: "gl_dj",
    category: "djibouti",
    tradeDirection: "IMPORT",
  },
  {
    code: "final_invoice",
    label: "Final Invoice",
    uploadedBy: "gl_dj",
    category: "djibouti",
    tradeDirection: "EXPORT",
  },
  {
    code: "final_invoice_slip",
    label: "Final Invoice Payment Slip",
    uploadedBy: "customer",
    category: "djibouti",
    tradeDirection: "EXPORT",
  },
];

/** Legacy single-type declaration codes (still shown when already uploaded). */
export const LEGACY_DECLARATION_FILE_CODES = ["im4", "im5", "ex3", "ex8"] as const;

/** Multi-file declaration uploads use `declaration_0`, `declaration_1`, … */
export const DECLARATION_FILE_PREFIX = "declaration_";

export function isDeclarationFileCode(code: string | null | undefined): boolean {
  if (!code) return false;
  const lower = code.toLowerCase();
  return (
    (LEGACY_DECLARATION_FILE_CODES as readonly string[]).includes(lower) ||
    lower === "declaration" ||
    lower.startsWith(DECLARATION_FILE_PREFIX)
  );
}

export interface ClearanceWorkflowFile {
  code: string;
  label: string;
  uploadedBy: ClearanceWorkflowFileOwner;
  category: ClearanceWorkflowFileCategory;
  file: { id: string; name: string; url: string } | null;
}

const CATALOG_BY_CODE = new Map(
  CLEARANCE_WORKFLOW_FILE_CATALOG.map((e) => [e.code, e]),
);

export function clearanceWorkflowFileLabel(code: string | null | undefined): string | null {
  if (!code) return null;
  return CATALOG_BY_CODE.get(code)?.label ?? null;
}

export function declarationFileLabel(code: string, index?: number): string {
  const lower = code.toLowerCase();
  const legacy = CATALOG_BY_CODE.get(lower);
  if (legacy?.category === "declaration") return legacy.label;
  if (lower.startsWith(DECLARATION_FILE_PREFIX) || lower === "declaration") {
    return index != null ? `Declaration document ${index + 1}` : "Customs declaration";
  }
  return code;
}

/** Legacy single import transit permit code. */
export const LEGACY_IMPORT_TRANSIT_PERMIT_CODE = "transit_permitted";

/** Multi-file import transit permit uploads use `transit_permit_0`, `transit_permit_1`, … */
export const TRANSIT_PERMIT_FILE_PREFIX = "transit_permit_";

export function isImportTransitPermitFileCode(code: string | null | undefined): boolean {
  if (!code) return false;
  const lower = code.toLowerCase();
  return (
    lower === LEGACY_IMPORT_TRANSIT_PERMIT_CODE || lower.startsWith(TRANSIT_PERMIT_FILE_PREFIX)
  );
}

export function transitPermitFileLabel(code: string, index?: number): string {
  const lower = code.toLowerCase();
  if (lower === LEGACY_IMPORT_TRANSIT_PERMIT_CODE) return "Transit Permit";
  if (lower.startsWith(TRANSIT_PERMIT_FILE_PREFIX)) {
    return index != null ? `Transit permit ${index + 1}` : "Transit Permit";
  }
  return code;
}

/** Legacy single T1 code (GL post-booking uploader). */
export const LEGACY_T1_TRANSPORT_CODE = "t1_transport_document";

/** Multi-file T1 transport uploads use `t1_transport_document_0`, `_1`, … */
export const T1_TRANSPORT_FILE_PREFIX = "t1_transport_document_";

export function isT1TransportFileCode(code: string | null | undefined): boolean {
  if (!code) return false;
  const lower = code.toLowerCase();
  return lower === LEGACY_T1_TRANSPORT_CODE || lower.startsWith(T1_TRANSPORT_FILE_PREFIX);
}

export function t1TransportFileLabel(code: string, index?: number): string {
  const lower = code.toLowerCase();
  if (lower === LEGACY_T1_TRANSPORT_CODE) return "T1 Transport Document";
  if (lower.startsWith(T1_TRANSPORT_FILE_PREFIX)) {
    return index != null ? `T1 transport document ${index + 1}` : "T1 Transport Document";
  }
  return code;
}

export const LEGACY_EXPORT_TRANSPORT_CODE = "export_transport_document";

export function isExportTransportFileCode(code: string | null | undefined): boolean {
  if (!code) return false;
  const lower = code.toLowerCase();
  return (
    lower === LEGACY_EXPORT_TRANSPORT_CODE ||
    lower.startsWith(`${LEGACY_EXPORT_TRANSPORT_CODE}_`)
  );
}

export function exportTransportFileLabel(code: string, index?: number): string {
  const lower = code.toLowerCase();
  if (lower === LEGACY_EXPORT_TRANSPORT_CODE) return "Transit Permit";
  if (lower.startsWith(`${LEGACY_EXPORT_TRANSPORT_CODE}_`)) {
    return index != null ? `Transit permit ${index + 1}` : "Transit Permit";
  }
  return code;
}

export function catalogEntriesForTradeDirection(
  tradeDirection: string,
): ClearanceWorkflowFileCatalogEntry[] {
  return CLEARANCE_WORKFLOW_FILE_CATALOG.filter(
    (e) => !e.tradeDirection || e.tradeDirection === tradeDirection,
  );
}
