import type {
  PurchaseOrder,
  PurchaseOrderLine,
  PurchasePriceList,
  PurchasePriceRule,
  PurchaseRequisition,
  PurchaseRequisitionLine,
  Schema,
} from "../lib/types";
import type {
  Company,
  Currency,
  Item,
  Partner,
  PartnerIdentification,
  PartnerRole,
  ReceiptEvidence,
  Site,
  Unit,
} from "./moduleMocks";

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

export const baseCompany = {
  id: "company-1",
  status: "ACTIVE",
  legalName: "Test Corp",
  baseCurrencyId: "currency-1",
  taxId: null,
  registrationNumber: null,
  street: "123 Main St",
  city: "New York",
  state: "NY",
  postalCode: "10001",
  country: "US",
  createdAt,
  updatedAt: null,
} as const satisfies Company;

export const baseSupplier = {
  id: "supplier-1",
  name: "Supplier One",
  type: "ORGANIZATION",
  companyId: baseCompany.id,
  preferredCurrencyId: null,
  status: "ACTIVE",
  createdAt,
  updatedAt: null,
} satisfies Partner;

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

export const baseSupplierRole = {
  id: "partner-role-1",
  partnerId: baseSupplier.id,
  role: "SUPPLIER",
  createdAt,
  updatedAt: null,
} satisfies PartnerRole;

export const baseSupplierIdentification = {
  id: "partner-identification-1",
  partnerId: baseSupplier.id,
  type: "TAX_ID",
  value: "TAX-DEFAULT",
  issuingCountry: null,
  validFrom: null,
  validTo: null,
  createdAt,
  updatedAt: null,
} satisfies PartnerIdentification;

export const baseReceivingSite = {
  id: "site-1",
  name: "Receiving Site",
  type: "WAREHOUSE",
  companyId: baseCompany.id,
  street: "123 Main St",
  city: "New York",
  state: "NY",
  postalCode: "10001",
  country: "US",
  timezone: "America/New_York",
  status: "ACTIVE",
  createdAt,
  updatedAt: null,
} satisfies Site;

export const baseCurrency = {
  id: "currency-1",
  code: "USD",
  name: "US Dollar",
  symbol: "$",
  decimalPlaces: 2,
  isBaseCurrency: true,
  status: "ACTIVE",
  createdAt,
  updatedAt: null,
} satisfies Currency;

export const baseUnit = {
  id: "unit-1",
  name: "Each",
  symbol: "EA",
  categoryId: "uom-category-1",
  conversionFactor: 1,
  roundingPrecision: 0,
  status: "ACTIVE",
  createdAt,
  updatedAt: null,
} satisfies Unit;

export const compatibleUnit = {
  id: "unit-2",
  name: "Box",
  symbol: "BOX",
  categoryId: "uom-category-1",
  conversionFactor: 12,
  roundingPrecision: 0,
  status: "ACTIVE",
  createdAt,
  updatedAt: null,
} satisfies Unit;

export const incompatibleUnit = {
  id: "unit-3",
  name: "Kilogram",
  symbol: "KG",
  categoryId: "uom-category-2",
  conversionFactor: 1,
  roundingPrecision: 3,
  status: "ACTIVE",
  createdAt,
  updatedAt: null,
} satisfies Unit;

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

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

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,
  orderStatus: "DRAFT",
  receiptStatus: null,
  billingStatus: null,
  orderDate: createdAt,
  externalSupplierOrderReference: "PO-EXT-1",
  supplierSnapshotName: baseSupplier.name,
  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,
  billedQuantity: null,
  createdAt,
  updatedAt: null,
} as const satisfies PurchaseOrderLine<Schema>;

export const basePurchaseOrderLines = [
  {
    itemId: baseItem.id,
    quantity: "10",
    unitPrice: "100",
    unitId: baseUnit.id,
    receivingSiteId: baseReceivingSite.id,
    requiresPhysicalReceipt: true,
  },
];

export const baseInventoryReceiptExecution = {
  id: "inventory-receipt-execution-1",
  companyId: baseCompany.id,
  supplierId: baseSupplier.id,
  receiptDate: createdAt,
  status: "DRAFT",
  createdAt,
  updatedAt: null,
} as const;

export const postedInventoryReceiptExecution = {
  ...baseInventoryReceiptExecution,
  id: "inventory-receipt-execution-2",
  status: "POSTED",
} as const;

export const baseReceiptEvidence = {
  purchaseOrderLineId: basePurchaseOrderLine.id,
  inventoryExecutionLineId: "inventory-execution-line-1",
  inventoryExecutionId: "inventory-execution-1",
  quantity: "4",
  createdAt,
} satisfies ReceiptEvidence;

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",
} as const satisfies PurchaseOrder<Schema>;

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