import type {
  StorageLocation,
  ValuationPolicy,
  StockLevel,
  StockReservation,
  Lot,
  LotStockLevel,
  SerialNumber,
  StockAdjustment,
  StockAdjustmentLine,
  TransferOrder,
  TransferOrderLine,
  ItemValuation,
  CostLayer,
  InventorySupplyPlan,
  InventoryExecution,
  InventoryExecutionLine,
  Schema,
} from "../lib/types";
import type { ItemManagementQueries } from "../module";

type ItemManagementItem = NonNullable<
  Awaited<ReturnType<ItemManagementQueries["getItem"]>>["value"]["item"]
>;

// Item fixtures (from item-management query contract)
export const baseActiveItem = {
  id: "item-1",
  sku: "SKU-001",
  name: "Active Widget",
  barcode: "BAR-001",
  unitId: "unit-ea",
  status: "ACTIVE",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies ItemManagementItem;

export const baseActiveItem2 = {
  ...baseActiveItem,
  id: "item-2",
  sku: "SKU-002",
  name: "Second Active Widget",
  barcode: "BAR-002",
} as const satisfies ItemManagementItem;

export const baseInactiveItem = {
  ...baseActiveItem,
  id: "item-3",
  sku: "SKU-003",
  name: "Inactive Widget",
  barcode: "BAR-003",
  status: "INACTIVE",
} as const satisfies ItemManagementItem;

// Site fixtures (minimal shape for cross-module tests)
export const baseActiveSite = {
  id: "site-1",
  name: "Tokyo DC",
  type: "WAREHOUSE",
  companyId: "company-1",
  street: "1-2-3 Shibuya",
  city: "Tokyo",
  state: "Tokyo",
  postalCode: "150-0002",
  country: "JP",
  timezone: "Asia/Tokyo",
  status: "ACTIVE",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const;

export const baseInactiveSite = {
  ...baseActiveSite,
  id: "site-3",
  name: "Closed DC",
  status: "INACTIVE",
} as const;

export const destinationSite = {
  ...baseActiveSite,
  id: "site-2",
  name: "Osaka DC",
} as const;

// StorageLocation fixtures
export const baseStorageLocation = {
  id: "location-1",
  name: "Main Shelf",
  code: "MAIN",
  siteId: "site-1",
  storageCondition: null,
  capacity: null,
  status: "ACTIVE",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StorageLocation<Schema>;

export const destinationStorageLocation = {
  id: "location-2",
  name: "Destination Shelf",
  code: "DEST",
  siteId: "site-2",
  storageCondition: null,
  capacity: null,
  status: "ACTIVE",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StorageLocation<Schema>;

export const transitStorageLocation = {
  id: "location-transit-1",
  name: "Transfer In Transit",
  code: "TRANSIT",
  siteId: "site-1",
  storageCondition: null,
  capacity: null,
  status: "ACTIVE",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StorageLocation<Schema>;

export const baseInactiveLocation = {
  id: "location-3",
  name: "Inactive Area",
  code: "OLD",
  siteId: "site-1",
  storageCondition: null,
  capacity: null,
  status: "INACTIVE",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StorageLocation<Schema>;

// ValuationPolicy fixtures
export const baseFifoPolicy = {
  id: "policy-1",
  name: "FIFO Policy",
  costingMethod: "FIFO",
  standardCostRate: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies ValuationPolicy<Schema>;

export const baseAvcoPolicy = {
  id: "policy-2",
  name: "AVCO Policy",
  costingMethod: "AVCO",
  standardCostRate: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies ValuationPolicy<Schema>;

export const baseStandardCostPolicy = {
  id: "policy-3",
  name: "Standard Cost Policy",
  costingMethod: "STANDARD_COST",
  standardCostRate: "10.5",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies ValuationPolicy<Schema>;

// StockLevel fixtures
export const baseStockLevel = {
  id: "stock-level-1",
  itemId: "item-1",
  storageLocationId: "location-1",
  stockType: "AVAILABLE",
  quantity: "85",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockLevel<Schema>;

export const baseBlockedStockLevel = {
  ...baseStockLevel,
  id: "stock-level-blocked-1",
  stockType: "BLOCKED",
  quantity: "5",
} as const satisfies StockLevel<Schema>;

export const baseStockReservation = {
  id: "stock-reservation-1",
  sourceType: "SALES_ORDER",
  sourceId: "sales-order-1",
  sourceLineId: "sales-order-line-1",
  itemId: "item-1",
  siteId: "site-1",
  storageLocationId: null,
  reservedQuantity: "10",
  consumedQuantity: "0",
  status: "OPEN",
  requiredDate: new Date("2024-01-15T00:00:00.000Z"),
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockReservation<Schema>;

export const emptyStockLevel = {
  id: "stock-level-2",
  itemId: "item-2",
  storageLocationId: "location-1",
  stockType: "AVAILABLE",
  quantity: "0",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockLevel<Schema>;

// Lot fixtures
export const baseActiveLot = {
  id: "lot-1",
  lotNumber: "LOT-001",
  itemId: "item-1",
  manufactureDate: new Date("2024-01-01T00:00:00.000Z"),
  expirationDate: new Date("2025-01-01T00:00:00.000Z"),
  supplierLotReference: "SUP-LOT-001",
  status: "ACTIVE",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies Lot<Schema>;

export const baseFullyConsumedLot = {
  id: "lot-2",
  lotNumber: "LOT-002",
  itemId: "item-1",
  manufactureDate: new Date("2023-06-01T00:00:00.000Z"),
  expirationDate: new Date("2024-06-01T00:00:00.000Z"),
  supplierLotReference: null,
  status: "FULLY_CONSUMED",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies Lot<Schema>;

// LotStockLevel fixtures
export const baseLotStockLevel = {
  id: "lot-stock-1",
  lotId: "lot-1",
  storageLocationId: "location-1",
  onHand: "50",
  blocked: "0",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies LotStockLevel<Schema>;

// SerialNumber fixtures
export const baseAvailableSerial = {
  id: "serial-1",
  serialNumber: "SN-001",
  itemId: "item-1",
  storageLocationId: "location-1",
  status: "AVAILABLE",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies SerialNumber<Schema>;

export const baseIssuedSerial = {
  id: "serial-2",
  serialNumber: "SN-002",
  itemId: "item-1",
  storageLocationId: "location-1",
  status: "ISSUED",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies SerialNumber<Schema>;

// ItemValuation fixtures
export const baseItemValuation = {
  id: "valuation-1",
  itemId: "item-1",
  valuationPolicyId: "policy-1",
  totalQuantity: "100",
  totalValue: "1000",
  costPerUnit: "10",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies ItemValuation<Schema>;

// CostLayer fixtures
export const baseCostLayer = {
  id: "cost-layer-1",
  itemId: "item-1",
  valuationPolicyId: "policy-1",
  inventoryLedgerId: "ledger-1",
  quantity: "100",
  unitCost: "10",
  remainingQuantity: "80",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies CostLayer<Schema>;

// StockAdjustment fixtures
export const baseDraftAdjustment = {
  id: "adjustment-1",
  adjustmentType: "CORRECTION",
  status: "DRAFT",
  reasonCode: "inventory-count",
  adjustmentDate: new Date("2024-01-01T00:00:00.000Z"),
  rejectionReason: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustment<Schema>;

export const baseSubmittedAdjustment = {
  id: "adjustment-2",
  adjustmentType: "CORRECTION",
  status: "SUBMITTED",
  reasonCode: "shrinkage",
  adjustmentDate: new Date("2024-01-02T00:00:00.000Z"),
  rejectionReason: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustment<Schema>;

export const baseRejectedAdjustment = {
  id: "adjustment-3",
  adjustmentType: "SCRAP",
  status: "REJECTED",
  reasonCode: "expired",
  adjustmentDate: new Date("2024-01-03T00:00:00.000Z"),
  rejectionReason: "need additional review",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustment<Schema>;

export const baseConfirmedAdjustment = {
  id: "adjustment-4",
  adjustmentType: "CORRECTION",
  status: "CONFIRMED",
  reasonCode: "shrinkage",
  adjustmentDate: new Date("2024-01-04T00:00:00.000Z"),
  rejectionReason: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustment<Schema>;

export const baseCancelledAdjustment = {
  id: "adjustment-5",
  adjustmentType: "CORRECTION",
  status: "CANCELLED",
  reasonCode: "entered-in-error",
  adjustmentDate: new Date("2024-01-05T00:00:00.000Z"),
  rejectionReason: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustment<Schema>;

// StockAdjustmentLine fixtures
export const correctionIncreaseLine = {
  id: "adj-line-1",
  stockAdjustmentId: "adjustment-1",
  itemId: "item-1",
  storageLocationId: "location-1",
  quantity: "5",
  adjustmentDirection: "INCREASE",
  fromStockCategory: null,
  unitCost: null,
  lotId: null,
  serialNumberId: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustmentLine<Schema>;

export const correctionDecreaseLine = {
  id: "adj-line-2",
  stockAdjustmentId: "adjustment-1",
  itemId: "item-1",
  storageLocationId: "location-1",
  quantity: "5",
  adjustmentDirection: "DECREASE",
  fromStockCategory: null,
  unitCost: null,
  lotId: null,
  serialNumberId: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustmentLine<Schema>;

export const scrapAvailableLine = {
  id: "adj-line-3",
  stockAdjustmentId: "adjustment-1",
  itemId: "item-1",
  storageLocationId: "location-1",
  quantity: "3",
  adjustmentDirection: null,
  fromStockCategory: "AVAILABLE",
  unitCost: null,
  lotId: null,
  serialNumberId: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustmentLine<Schema>;

export const scrapBlockedLine = {
  id: "adj-line-4",
  stockAdjustmentId: "adjustment-1",
  itemId: "item-1",
  storageLocationId: "location-1",
  quantity: "3",
  adjustmentDirection: null,
  fromStockCategory: "BLOCKED",
  unitCost: null,
  lotId: null,
  serialNumberId: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustmentLine<Schema>;

export const blockLine = {
  id: "adj-line-5",
  stockAdjustmentId: "adjustment-1",
  itemId: "item-1",
  storageLocationId: "location-1",
  quantity: "10",
  adjustmentDirection: null,
  fromStockCategory: null,
  unitCost: null,
  lotId: null,
  serialNumberId: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustmentLine<Schema>;

export const unblockLine = {
  id: "adj-line-6",
  stockAdjustmentId: "adjustment-1",
  itemId: "item-1",
  storageLocationId: "location-1",
  quantity: "5",
  adjustmentDirection: null,
  fromStockCategory: null,
  unitCost: null,
  lotId: null,
  serialNumberId: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies StockAdjustmentLine<Schema>;

// TransferOrder fixtures
export const baseDraftTransferOrder = {
  id: "transfer-order-1",
  sourceSiteId: "site-1",
  destinationSiteId: "site-2",
  plannedShipmentDate: new Date("2024-01-15T00:00:00.000Z"),
  expectedReceiptDate: new Date("2024-01-17T00:00:00.000Z"),
  status: "DRAFT",
  closedAt: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies TransferOrder<Schema>;

export const baseOpenTransferOrder = {
  ...baseDraftTransferOrder,
  id: "transfer-order-3",
  status: "OPEN",
} as const satisfies TransferOrder<Schema>;

export const baseClosedTransferOrder = {
  ...baseDraftTransferOrder,
  id: "transfer-order-4",
  status: "CLOSED",
  closedAt: new Date("2024-01-10T00:00:00.000Z"),
} as const satisfies TransferOrder<Schema>;

export const baseTransferOrderLine = {
  id: "transfer-order-line-1",
  transferOrderId: "transfer-order-1",
  itemId: "item-1",
  unitId: "unit-ea",
  orderedQuantity: "10",
  shippedQuantity: "0",
  receivedQuantity: "0",
  lotId: null,
  serialNumberId: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies TransferOrderLine<Schema>;

export const partiallyReceivedTransferOrderLine = {
  ...baseTransferOrderLine,
  id: "transfer-order-line-2",
  transferOrderId: "transfer-order-3",
  shippedQuantity: "10",
  receivedQuantity: "4",
} as const satisfies TransferOrderLine<Schema>;

export const fullyReceivedTransferOrderLine = {
  ...baseTransferOrderLine,
  id: "transfer-order-line-3",
  transferOrderId: "transfer-order-3",
  shippedQuantity: "10",
  receivedQuantity: "10",
} as const satisfies TransferOrderLine<Schema>;

// InventorySupplyPlan fixtures
export const baseSupplyPlan = {
  id: "supply-plan-1",
  sourceType: "PURCHASE_ORDER",
  sourceId: "po-1",
  sourceLineId: "po-line-1",
  itemId: "item-1",
  siteId: "site-1",
  expectedQuantity: "100",
  receivedQuantity: "0",
  status: "OPEN",
  expectedDate: new Date("2026-06-01T00:00:00.000Z"),
  lotId: null,
  serialNumberId: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies InventorySupplyPlan<Schema>;

export const baseGoodsReceiptExecution = {
  id: "execution-1",
  executionType: "GOODS_RECEIPT",
  status: "DRAFT",
  effectiveDate: new Date("2024-01-10T00:00:00.000Z"),
  postedAt: null,
  sourceDocumentType: "PURCHASE_ORDER",
  sourceDocumentId: "po-1",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies InventoryExecution<Schema>;

export const baseGoodsReceiptExecutionLine = {
  id: "execution-line-1",
  inventoryExecutionId: "execution-1",
  direction: "IN",
  itemId: "item-1",
  quantity: "5",
  unitId: "unit-ea",
  primaryQuantity: "5",
  primaryUnitId: "unit-ea",
  unitConversionRate: "1",
  unitCost: "12",
  storageLocationId: "location-1",
  stockType: "AVAILABLE",
  sourceLineId: "po-line-1",
  lotId: null,
  serialNumberId: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies InventoryExecutionLine<Schema>;

export const baseGoodsIssueExecution = {
  id: "execution-issue-1",
  executionType: "GOODS_ISSUE",
  status: "DRAFT",
  effectiveDate: new Date("2024-01-10T00:00:00.000Z"),
  postedAt: null,
  sourceDocumentType: "SALES_ORDER",
  sourceDocumentId: "so-1",
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies InventoryExecution<Schema>;

export const baseGoodsIssueExecutionLine = {
  id: "execution-issue-line-1",
  inventoryExecutionId: "execution-issue-1",
  direction: "OUT",
  itemId: "item-1",
  quantity: "5",
  unitId: "unit-ea",
  primaryQuantity: "5",
  primaryUnitId: "unit-ea",
  unitConversionRate: "1",
  unitCost: null,
  storageLocationId: "location-1",
  stockType: "AVAILABLE",
  sourceLineId: "so-line-1",
  lotId: null,
  serialNumberId: null,
  createdAt: new Date("2024-01-01T00:00:00.000Z"),
  updatedAt: null,
} as const satisfies InventoryExecutionLine<Schema>;
