/** 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;
  /** `PoResult.status` receive workflow (pending / receiving / completed). */
  status: "pending" | "receiving" | "completed";
}

/** @deprecated Use {@link PoWorkbenchListRow}. */
export type WorkbenchMockSearchResult = PoWorkbenchListRow;

/** 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; 預計送貨/到貨日期 = 當地今天（起訖同天）。 */
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();
