import { cache } from "react";
import "client-only";
// import { serverFetchJson } from "@/app/utils/fetchUtil";
// import { BASE_API_URL } from "@/config/api";
import { serverFetchJson } from "../../utils/fetchUtil";
import { BASE_API_URL } from "../../../config/api";
import { Uom } from "../settings/uom";
import { RecordsRes } from "../utils";
import { QcFormInput, QcResult } from "../qc";
import { EscalationResult } from "../escalation";

export enum StockInStatus {
  PENDING = "pending",
  RECEIVED = "received",
  APPROVED = "escalated",
  REJECTED = "rejected",
  ESCALATED = "escalated",
  COMPLETED = "completed",
  PARTIALLY_COMPLETED = "partially_completed",
}

export interface StockInLineInput {
  id?: number;
  itemId?: number;
  acceptedQty?: number;
  receivedQty?: number;
  status?: string;
  expiryDate?: string;
  productLotNo?: string;
  receiptDate?: string;
  dnDate?: number[];
  dnNo?: string;
}

export interface StockInInput {
  stockInId?: number;
  poCode?: string;
  productLotNo?: string;
  dnNo?: string;
  dnDate?: string;
  itemName?: string;
  lotNo?: string;
  invoiceNo?: string;
  receiptDate: string;
  supplier?: string;
  acceptedQty: number;
  qty: number;
  receivedQty: number;
  acceptedWeight?: number;
  productionDate?: string;
  expiryDate: string;
  uom?: Uom;
}

export interface PoResult {
  id: number;
  code: string;
  orderDate: string;
  supplier: string;
  estimatedArrivalDate: string;
  completedDate: string;
  itemDetail?: string;
  itemCode?: string;
  itemName?: string;
  itemQty?: string;
  itemSumAcceptedQty?: string;
  itemUom?: string;
  escalated: boolean;
  status: string;
  pol?: PurchaseOrderLine[];
}

export interface PurchaseOrderLine {
  id: number;
  purchaseOrderId: number;
  itemId: number;
  itemNo: string;
  itemName: string;
  qty: number;
  processed: number;
  receivedQty: number;
  uom: Uom;
  stockUom: StockUomForPoLine; 
  price: number;
  status: string;
  stockInLine: StockInLine[];
}

export interface StockUomForPoLine {
  id: number;
  stockUomCode: string;
  stockUomDesc: string;
  stockQty: number;
  stockRatioN: number;
  stockRatioD: number;
  purchaseRatioN: number;
  purchaseRatioD: number;
}

export interface StockInLine {
  id: number;
  stockInId?: number;
  purchaseOrderId?: number;
  purchaseOrderLineId: number;
  jobOrderId: number;
  itemId: number;
  itemNo: string;
  itemName: string;
  itemType: string;
  demandQty: number;
  acceptedQty: number;
  purchaseDemandQty?: number;
  purchaseAcceptedQty?: number;
  qty?: number;
  receivedQty?: number;
  processed?: number;
  price?: number;
  priceUnit?: string;
  shelfLife?: number;
  receiptDate?: string;
  productionDate?: string;
  productLotNo?: string;
  expiryDate?: string;
  status: string;
  supplier?: string;
  lotNo?: string;
  poCode?: string;
  uom?: Uom;
  purchaseUomDesc?: string;
  stockUomDesc?: string;
  joCode?: string;
  warehouseCode?: string;
  defaultWarehouseId: number; // id for now
  locationCode?: string;
  dnNo?: string;
  dnDate?: number[];
  stockQty?: number;
  bomDescription?: string;
  handlerId?: number;
  putAwayLines?: PutAwayLine[];
  qcResult?: QcResult[];
  escResult?: EscalationResult[];
}

export interface EscalationInput {
  status: string;
  remarks?: string;
  reason?: string;
  handlerId: number;
  productLotNo?: string;
  acceptedQty?: number; // this is the qty to be escalated
  // escalationQty: number
}
export interface PutAwayLine {
  id?: number
  qty: number
  stockQty?: number
  warehouseId: number;
  warehouse: string;
  printQty: number;
  _isNew?: boolean;
}
export interface PutAwayInput {
  status: string;
  acceptedQty: number;
  warehouseId: number;
  putAwayLines: PutAwayLine[]
}

export type ModalFormInput = Partial<
  QcFormInput & StockInInput & PutAwayInput
> & {
  escalationLog? : Partial<EscalationInput>
};