/**
 * ANCPI ePay integration types.
 */

/* ── Config ──────────────────────────────────────────────────────── */

export type EpayConfig = {
  username: string;
  password: string;
  baseUrl: string; // https://epay.ancpi.ro/epay
  loginUrl: string; // https://oassl.ancpi.ro/openam/UI/Login
  defaultSolicitantId: string; // "14452"
};

/* ── API Responses ───────────────────────────────────────────────── */

export type EpayCartItem = {
  id: number; // basketRowId
  productId: number;
  sku?: string;
  custom2?: string;
  custom5?: string;
};

export type EpayCartResponse = {
  items: EpayCartItem[];
  numberOfItems: number;
};

export type EpaySearchResult = {
  countyId: string;
  immovableId: string;
  immovableTypeCode: string; // T=Teren, C=Constructie, A=Apartament
  electronicIdentifier: string;
  previousCadNo: string | null;
  topographicNo: string | null;
  measureadArea: string | null; // typo is in ePay API
  legalArea: string | null;
  status: string; // "Activa"
  landBookType: { code: string };
  hasGraphics: boolean;
  address: string;
  previousLandBookNo: string | null;
};

export type EpayUatEntry = {
  id: number; // real ePay UAT ID (NOT simple index)
  value: string; // UAT name
};

export type EpaySolutionDoc = {
  idDocument: number;
  idTipDocument: number | null;
  nume: string; // "Extras_Informare_65297.pdf"
  numar: string | null;
  serie: string | null;
  dataDocument: string; // "2026-03-22"
  contentType: string;
  linkDownload: string;
  downloadValabil: boolean;
  valabilNelimitat: boolean;
  zileValabilitateDownload: number;
  transactionId: number;
};

export type EpayOrderStatus = {
  orderId: string;
  status: string; // "Receptionata" | "In curs de procesare" | "Finalizata" | "Anulata"
  documents: EpaySolutionDoc[];
  /** CF number → document mapping (for correct batch assignment) */
  documentsByCadastral: Map<string, EpaySolutionDoc>;
};

/* ── Domain Types ────────────────────────────────────────────────── */

export type CfExtractStatus =
  | "pending"
  | "queued"
  | "cart"
  | "searching"
  | "ordering"
  | "polling"
  | "downloading"
  | "completed"
  | "failed"
  | "cancelled";

export type CfExtractCreateInput = {
  nrCadastral: string;
  nrCF?: string;
  siruta?: string;
  judetIndex: number;
  judetName: string;
  uatId: number;
  uatName: string;
  gisFeatureId?: string;
  prodId?: number;
};

export type OrderMetadata = {
  basketRowId: number;
  judetIndex: number;
  uatId: number;
  nrCF: string;
  nrCadastral: string;
  solicitantId: string;
};
