import type {
  GoodsReceipt,
  GoodsReceiptLine,
  PurchaseBill,
  PurchaseBillLine,
  PurchaseOrder,
  PurchaseOrderLine,
  PurchasePaymentTerm,
  PurchasePriceList,
  PurchasePriceRule,
  PurchaseRequisition,
  PurchaseRequisitionLine,
  Schema,
  SupplierProfile,
} from "../lib/types";

const createdAt = new Date("2024-01-01T00:00:00.000Z");

export const baseCompany = {
  id: "company-1",
  status: "ACTIVE",
};

export const baseSupplier = {
  id: "supplier-1",
  companyId: baseCompany.id,
  name: "Supplier One",
  status: "ACTIVE",
};

export const inactiveSupplier = {
  ...baseSupplier,
  id: "supplier-2",
  status: "INACTIVE",
};

export const baseSupplierRole = {
  id: "partner-role-1",
  partnerId: baseSupplier.id,
  role: "SUPPLIER",
};

export const baseReceivingSite = {
  id: "site-1",
  companyId: baseCompany.id,
  status: "ACTIVE",
};

export const baseCurrency = {
  id: "currency-1",
  code: "USD",
  status: "ACTIVE" as const,
};

export const baseUnit = {
  id: "unit-1",
  categoryId: "uom-category-1",
  symbol: "EA",
  status: "ACTIVE" as const,
};

export const compatibleUnit = {
  id: "unit-2",
  categoryId: "uom-category-1",
  symbol: "BOX",
  status: "ACTIVE" as const,
};

export const incompatibleUnit = {
  id: "unit-3",
  categoryId: "uom-category-2",
  symbol: "KG",
  status: "ACTIVE" as const,
};

export const baseItem = {
  id: "item-1",
  unitId: baseUnit.id,
  status: "ACTIVE",
  name: "Base Item",
  sku: "ITEM-001",
};

export const inactiveItem = {
  ...baseItem,
  id: "item-2",
  status: "INACTIVE",
};

export const nullLineFields = {
  fixedDayOfMonth: null,
  percentage: null,
  fixedAmount: null,
  note: null,
} as const;

export const baseScheduleLines = [{ ...nullLineFields, kind: "NET_DAYS" as const, days: 30 }];

export const installmentScheduleLines = [
  { ...nullLineFields, kind: "INSTALLMENT" as const, days: 15, percentage: "50" },
  { ...nullLineFields, kind: "INSTALLMENT" as const, days: 30, percentage: "50" },
];

export const basePaymentTerm = {
  id: "payment-term-1",
  companyId: baseCompany.id,
  name: "Net 30",
  code: "NET30",
  status: "ACTIVE",
  notes: null,
  createdAt,
  updatedAt: null,
} as const satisfies PurchasePaymentTerm<Schema>;

export const basePaymentTermLine = {
  id: "payment-term-line-1",
  purchasePaymentTermId: basePaymentTerm.id,
  kind: "NET_DAYS",
  days: 30,
  fixedDayOfMonth: null,
  percentage: null,
  fixedAmount: null,
  note: null,
  createdAt,
  updatedAt: null,
} as const;

const basePaymentTermSnapshotLines: PurchaseOrder<Schema>["paymentTermSnapshotLines"] =
  baseScheduleLines.map((l) => ({
    kind: l.kind as string,
    days: l.days as number | null,
    fixedDayOfMonth: l.fixedDayOfMonth,
    percentage: l.percentage,
    fixedAmount: l.fixedAmount,
    note: l.note,
  }));

export const inactivePaymentTerm = {
  ...basePaymentTerm,
  id: "payment-term-2",
  status: "INACTIVE",
} as const satisfies PurchasePaymentTerm<Schema>;

export const baseSupplierProfile = {
  id: "supplier-profile-1",
  companyId: baseCompany.id,
  supplierId: baseSupplier.id,
  defaultPurchasePaymentTermId: basePaymentTerm.id,
  createdAt,
  updatedAt: null,
} as const satisfies SupplierProfile<Schema>;

export const basePriceList = {
  id: "price-list-1",
  companyId: baseCompany.id,
  supplierId: baseSupplier.id,
  name: "Default Supplier Contract",
  code: "PL-001",
  priority: 10,
  status: "ACTIVE",
  notes: null,
  createdAt,
  updatedAt: null,
} as const satisfies PurchasePriceList<Schema>;

export const inactivePriceList = {
  ...basePriceList,
  id: "price-list-2",
  status: "INACTIVE",
} as const satisfies PurchasePriceList<Schema>;

export const basePriceRule = {
  id: "price-rule-1",
  priceListId: basePriceList.id,
  itemId: baseItem.id,
  currencyId: baseCurrency.id,
  unitId: baseUnit.id,
  minimumQuantity: "0",
  unitPrice: "100",
  effectiveFrom: createdAt,
  effectiveTo: null,
  priority: 10,
  status: "ACTIVE",
  notes: null,
  createdAt,
  updatedAt: null,
} as const satisfies PurchasePriceRule<Schema>;

export const higherBreakPriceRule = {
  ...basePriceRule,
  id: "price-rule-2",
  minimumQuantity: "10",
  unitPrice: "90",
  priority: 20,
} as const satisfies PurchasePriceRule<Schema>;

export const inactivePriceRule = {
  ...basePriceRule,
  id: "price-rule-3",
  status: "INACTIVE",
} as const satisfies PurchasePriceRule<Schema>;

export const basePurchaseOrder = {
  id: "purchase-order-1",
  companyId: baseCompany.id,
  supplierId: baseSupplier.id,
  receivingSiteId: baseReceivingSite.id,
  paymentTermId: basePaymentTerm.id,
  orderStatus: "DRAFT",
  receiptStatus: null,
  billingStatus: null,
  orderDate: createdAt,
  externalSupplierOrderReference: "PO-EXT-1",
  supplierSnapshotName: baseSupplier.name,
  paymentTermSnapshotLines: basePaymentTermSnapshotLines,
  revisionNumber: 1,
  rejectionReason: null,
  closeReason: null,
  createdAt,
  updatedAt: null,
} as const satisfies PurchaseOrder<Schema>;

export const submittedPurchaseOrder = {
  ...basePurchaseOrder,
  id: "purchase-order-2",
  orderStatus: "SUBMITTED",
} as const satisfies PurchaseOrder<Schema>;

export const orderedPurchaseOrder = {
  ...basePurchaseOrder,
  id: "purchase-order-3",
  orderStatus: "ORDERED",
  receiptStatus: "NOT_RECEIVED",
  billingStatus: "NOT_BILLED",
} as const satisfies PurchaseOrder<Schema>;

export const basePurchaseOrderLine = {
  id: "po-line-1",
  purchaseOrderId: orderedPurchaseOrder.id,
  itemId: baseItem.id,
  itemSnapshotName: baseItem.name,
  itemSnapshotSku: baseItem.sku,
  quantity: "10",
  unitPrice: "100",
  unitId: baseUnit.id,
  receivingSiteId: baseReceivingSite.id,
  requiresPhysicalReceipt: true,
  createdAt,
  updatedAt: null,
} as const satisfies PurchaseOrderLine<Schema>;

/** Legacy array alias kept for command input tests */
export const basePurchaseOrderLines = [
  {
    itemId: baseItem.id,
    quantity: "10",
    unitPrice: "100",
    unitId: baseUnit.id,
    receivingSiteId: baseReceivingSite.id,
    requiresPhysicalReceipt: true,
  },
];

export const baseGoodsReceipt = {
  id: "goods-receipt-1",
  companyId: baseCompany.id,
  supplierId: baseSupplier.id,
  receiptDate: createdAt,
  status: "DRAFT",
  createdAt,
  updatedAt: null,
} as const satisfies GoodsReceipt<Schema>;

export const postedGoodsReceipt = {
  ...baseGoodsReceipt,
  id: "goods-receipt-2",
  status: "POSTED",
} as const satisfies GoodsReceipt<Schema>;

export const baseGoodsReceiptLine = {
  id: "gr-line-1",
  goodsReceiptId: baseGoodsReceipt.id,
  purchaseOrderLineId: basePurchaseOrderLine.id,
  itemId: baseItem.id,
  quantity: "4",
  unitId: baseUnit.id,
  requiresInventoryHandoff: true,
  destinationSite: baseReceivingSite.id,
  destinationStorageLocation: "dock-1",
  createdAt,
  updatedAt: null,
} as const satisfies GoodsReceiptLine<Schema>;

/** Legacy array alias kept for command input tests */
export const baseGoodsReceiptLines = [
  {
    purchaseOrderLineId: basePurchaseOrderLine.id,
    quantity: "4",
    itemId: baseItem.id,
    unitId: baseUnit.id,
    requiresInventoryHandoff: true,
    destinationSite: baseReceivingSite.id,
    destinationStorageLocation: "dock-1",
  },
];

export const basePurchaseBill = {
  id: "purchase-bill-1",
  companyId: baseCompany.id,
  supplierId: baseSupplier.id,
  invoiceNumber: "INV-001",
  invoiceDate: createdAt,
  status: "DRAFT",
  paymentTermSnapshotLines: basePaymentTermSnapshotLines,
  supplierTaxIdentification: "TAX-001",
  matchingMode: null,
  varianceReason: null,
  releaseReason: null,
  dueSchedule: null,
  createdAt,
  updatedAt: null,
} as const satisfies PurchaseBill<Schema>;

export const blockedPurchaseBill = {
  ...basePurchaseBill,
  id: "purchase-bill-2",
  status: "BLOCKED",
  varianceReason: "Price variance",
} as const satisfies PurchaseBill<Schema>;

export const matchedPurchaseBill = {
  ...basePurchaseBill,
  id: "purchase-bill-3",
  status: "MATCHED",
  matchingMode: "TWO_WAY",
  dueSchedule: [{ dueDate: createdAt, amount: "440" }],
} as const satisfies PurchaseBill<Schema>;

export const basePurchaseBillLine = {
  id: "bill-line-1",
  purchaseBillId: basePurchaseBill.id,
  purchaseOrderLineId: basePurchaseOrderLine.id,
  quantity: "4",
  billedUnitPrice: "100",
  unitId: baseUnit.id,
  taxCode: "VAT",
  taxableAmount: "400",
  taxAmount: "40",
  createdAt,
  updatedAt: null,
} as const satisfies PurchaseBillLine<Schema>;

/** Legacy array alias kept for command input tests */
export const basePurchaseBillLines = [
  {
    purchaseOrderLineId: basePurchaseOrderLine.id,
    quantity: "4",
    billedUnitPrice: "100",
    unitId: baseUnit.id,
    taxCode: "VAT",
    taxableAmount: "400",
    taxAmount: "40",
  },
];

export const basePurchaseRequisition = {
  id: "purchase-requisition-1",
  companyId: baseCompany.id,
  requesterId: "requester-1",
  status: "DRAFT",
  justification: "Office supplies needed",
  desiredDeliveryDate: null,
  rejectionReason: null,
  closeReason: null,
  createdAt,
  updatedAt: null,
} as const satisfies PurchaseRequisition<Schema>;

export const submittedPurchaseRequisition = {
  ...basePurchaseRequisition,
  id: "purchase-requisition-2",
  status: "SUBMITTED",
} as const satisfies PurchaseRequisition<Schema>;

export const approvedPurchaseRequisition = {
  ...basePurchaseRequisition,
  id: "purchase-requisition-3",
  status: "APPROVED",
} as const satisfies PurchaseRequisition<Schema>;

export const convertedPurchaseRequisition = {
  ...basePurchaseRequisition,
  id: "purchase-requisition-4",
  status: "CONVERTED",
} as const satisfies PurchaseRequisition<Schema>;

export const cancelledPurchaseRequisition = {
  ...basePurchaseRequisition,
  id: "purchase-requisition-5",
  status: "CANCELLED",
} as const satisfies PurchaseRequisition<Schema>;

export const closedPurchaseRequisition = {
  ...basePurchaseRequisition,
  id: "purchase-requisition-6",
  status: "CLOSED",
} as const satisfies PurchaseRequisition<Schema>;

export const basePurchaseRequisitionLine = {
  id: "pr-line-1",
  purchaseRequisitionId: basePurchaseRequisition.id,
  description: "Printer paper A4",
  quantity: "10",
  estimatedUnitPrice: "5",
  itemId: baseItem.id,
  unitId: baseUnit.id,
  suggestedSupplierId: null,
  needByDate: null,
  createdAt,
  updatedAt: null,
} as const satisfies PurchaseRequisitionLine<Schema>;

export const basePurchaseRequisitionLines = [
  {
    description: "Printer paper A4",
    quantity: "10",
    estimatedUnitPrice: "5",
    itemId: baseItem.id,
    unitId: baseUnit.id,
    suggestedSupplierId: null,
    needByDate: null,
  },
];

// --- Amendment fixtures ---

export const orderedPurchaseOrderWithRevision = {
  ...orderedPurchaseOrder,
  id: "purchase-order-10",
  revisionNumber: 1,
} as const satisfies PurchaseOrder<Schema>;

export const poLineForAmendment = {
  ...basePurchaseOrderLine,
  id: "po-line-10",
  purchaseOrderId: orderedPurchaseOrderWithRevision.id,
} as const satisfies PurchaseOrderLine<Schema>;
