/** `PoResult.status` receive workflow (pending / receiving / completed). */
export type PoWorkbenchReceiveStatus = "pending" | "receiving" | "completed";

/** Item category shown in the workbench detail grid (mock / future API). */
export type PoWorkbenchItemCategory =
  | "material"
  | "finishedGoods"
  | "semiFinished"
  | "consumable"
  | "miscNonConsumable";

/** One row in the workbench list UI; populated from `mapPoResultToListRow` and `/po/list`. */
export interface PoWorkbenchListRow {
  id: string;
  poNumber: string;
  supplierName: string;
  /** ISO calendar date `YYYY-MM-DD` (or parseable for display). */
  orderDate: string;
  /** ISO calendar date `YYYY-MM-DD` (or parseable for display). */
  estimatedArrivalDate: string;
  /** Same as `PoResult.escalated`. */
  escalated: boolean;
  status: PoWorkbenchReceiveStatus;
}

/** One line in the workbench detail grid (item column wired first). */
export interface PoWorkbenchDetailsGridLineRow {
  id: string;
  itemName: string;
  /** Two letters + four digits, e.g. `AB1234`. */
  itemCode: string;
  category: PoWorkbenchItemCategory;
  /** Format `YF-WYYY-#X-YY` (X = letter, Y = digit). */
  storageLocation: string;
  receiveStatus: PoWorkbenchReceiveStatus;
  /** Expected receive quantity for the line (display in qty column). */
  expectedQty: number;
  /** Purchase UoM breakdown, e.g. `1箱X100包X150克`. */
  purchaseUnitLabel: string;
  /** Received qty in purchase UoM (實收 · 採購單位). */
  receivedQtyPurchase: number;
  /** Received qty converted to inventory UoM (實收 · 庫存單位). */
  receivedQtyInventory: number;
  /** Inventory UoM label, e.g. `克` or `個`. */
  inventoryUnitLabel: string;
}

/** Matches `PoResult.escalated` filtering on the PO search screen. */
export type ReportStatusFilter = "ALL" | "ESCALATED" | "NOT_ESCALATED";

/** Matches `PoResult.status` values used for PO 來貨狀態 (pending / receiving / completed). */
export type ReceiveStatusFilter = "ALL" | "pending" | "receiving" | "completed";

export const PO_WORKBENCH_ESCALATION_FILTER_OPTIONS = [
  { value: "ESCALATED" as const, labelKey: "Escalated" },
  { value: "NOT_ESCALATED" as const, labelKey: "NotEscalated" },
] as const;

export const PO_WORKBENCH_RECEIVE_STATUS_OPTIONS = [
  { value: "pending" as const, labelKey: "pending" },
  { value: "receiving" as const, labelKey: "receiving" },
  { value: "completed" as const, labelKey: "completed" },
] as const;

export interface PoWorkbenchAdvancedFilters {
  supplierQuery: string;
  orderDateFrom: string;
  orderDateTo: string;
  etaDateFrom: string;
  etaDateTo: string;
  reportStatus: ReportStatusFilter;
  receiveStatus: ReceiveStatusFilter;
}

function pad2(n: number): string {
  return n < 10 ? `0${n}` : String(n);
}

/** Local calendar `YYYY-MM-DD` (e.g. default ETA "today" in the workbench). */
export function getLocalDateYmd(date: Date = new Date()): string {
  return `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(
    date.getDate(),
  )}`;
}

/** Default: no supplier/order filters; ETA range = local today (from–to). */
export function createDefaultAdvancedFilters(): PoWorkbenchAdvancedFilters {
  const today = getLocalDateYmd();
  return {
    supplierQuery: "",
    orderDateFrom: "",
    orderDateTo: "",
    etaDateFrom: today,
    etaDateTo: today,
    reportStatus: "ALL",
    receiveStatus: "ALL",
  };
}

export const DEFAULT_ADVANCED_FILTERS: PoWorkbenchAdvancedFilters =
  createDefaultAdvancedFilters();
