import {
  createGetDB,
  type Generated,
  type Timestamp,
  type NamespaceDB,
  type NamespaceInsertable,
  type NamespaceSelectable,
  type NamespaceTable,
  type NamespaceTableName,
  type NamespaceTransaction,
  type NamespaceUpdateable,
} from "@tailor-platform/sdk/kysely";

export interface Namespace {
  "main-db": {
    CostLayer: {
      id: Generated<string>;
      itemId: string;
      valuationPolicyId: string;
      inventoryLedgerId: string;
      quantity: string;
      unitCost: string;
      remainingQuantity: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    InventoryExecution: {
      id: Generated<string>;
      executionType: "GOODS_RECEIPT" | "GOODS_ISSUE" | "STOCK_ADJUSTMENT";
      status: "DRAFT" | "CANCELLED" | "POSTED";
      effectiveDate: Timestamp;
      postedAt: Timestamp | null;
      sourceDocumentType: "PURCHASE_ORDER" | "SALES_ORDER" | "TRANSFER_ORDER" | "MANUFACTURING_ORDER" | "STOCK_ADJUSTMENT" | null;
      sourceDocumentId: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    InventoryExecutionLine: {
      id: Generated<string>;
      inventoryExecutionId: string;
      direction: "IN" | "OUT";
      itemId: string;
      quantity: string;
      unitId: string;
      primaryQuantity: string;
      primaryUnitId: string;
      unitConversionRate: string;
      unitCost: string | null;
      storageLocationId: string;
      stockType: "AVAILABLE" | "BLOCKED" | "IN_TRANSIT";
      sourceLineId: string | null;
      lotId: string | null;
      serialNumberId: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    InventoryLedger: {
      id: Generated<string>;
      inventoryExecutionId: string;
      dcIndicator: "DR" | "CR";
      itemId: string;
      storageLocationId: string;
      stockType: "AVAILABLE" | "BLOCKED" | "IN_TRANSIT";
      quantity: string;
      executedAt: Timestamp;
      lotId: string | null;
      serialNumberId: string | null;
      createdAt: Generated<Timestamp>;
    }

    InventorySupplyPlan: {
      id: Generated<string>;
      sourceType: "PURCHASE_ORDER" | "TRANSFER_ORDER" | "MANUFACTURING_ORDER";
      sourceId: string;
      sourceLineId: string;
      itemId: string;
      siteId: string;
      expectedQuantity: string;
      receivedQuantity: string;
      status: "OPEN" | "CLOSED";
      expectedDate: Timestamp;
      lotId: string | null;
      serialNumberId: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ItemValuation: {
      id: Generated<string>;
      itemId: string;
      valuationPolicyId: string;
      totalQuantity: string;
      totalValue: string;
      costPerUnit: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    Lot: {
      id: Generated<string>;
      lotNumber: string;
      itemId: string;
      manufactureDate: Timestamp | null;
      expirationDate: Timestamp | null;
      supplierLotReference: string | null;
      status: "ACTIVE" | "FULLY_CONSUMED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    LotStockLevel: {
      id: Generated<string>;
      lotId: string;
      storageLocationId: string;
      onHand: string;
      blocked: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    SerialNumber: {
      id: Generated<string>;
      serialNumber: string;
      itemId: string;
      storageLocationId: string;
      status: "AVAILABLE" | "ISSUED" | "RETURNED" | "SCRAPPED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    StockAdjustment: {
      id: Generated<string>;
      adjustmentType: "CORRECTION" | "SCRAP" | "BLOCK" | "UNBLOCK";
      status: "DRAFT" | "SUBMITTED" | "CANCELLED" | "CONFIRMED" | "REJECTED";
      reasonCode: string;
      adjustmentDate: Timestamp;
      rejectionReason: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    StockAdjustmentLine: {
      id: Generated<string>;
      stockAdjustmentId: string;
      itemId: string;
      storageLocationId: string;
      quantity: string;
      adjustmentDirection: "INCREASE" | "DECREASE" | null;
      fromStockCategory: "AVAILABLE" | "BLOCKED" | null;
      unitCost: string | null;
      lotId: string | null;
      serialNumberId: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    StockLevel: {
      id: Generated<string>;
      itemId: string;
      storageLocationId: string;
      stockType: "AVAILABLE" | "BLOCKED" | "IN_TRANSIT";
      quantity: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    StockReservation: {
      id: Generated<string>;
      sourceType: "SALES_ORDER" | "TRANSFER_ORDER" | "MANUFACTURING_ORDER";
      sourceId: string;
      sourceLineId: string;
      itemId: string;
      siteId: string;
      storageLocationId: string | null;
      reservedQuantity: string;
      consumedQuantity: string;
      status: "OPEN" | "CLOSED";
      requiredDate: Timestamp;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    StorageLocation: {
      id: Generated<string>;
      name: string;
      code: string | null;
      siteId: string;
      storageCondition: string | null;
      capacity: string | null;
      status: "ACTIVE" | "INACTIVE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    TransferOrder: {
      id: Generated<string>;
      sourceSiteId: string;
      destinationSiteId: string;
      plannedShipmentDate: Timestamp;
      expectedReceiptDate: Timestamp;
      status: "DRAFT" | "OPEN" | "CLOSED";
      closedAt: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    TransferOrderLine: {
      id: Generated<string>;
      transferOrderId: string;
      itemId: string;
      unitId: string;
      orderedQuantity: string;
      shippedQuantity: string;
      receivedQuantity: string;
      lotId: string | null;
      serialNumberId: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ValuationPolicy: {
      id: Generated<string>;
      name: string;
      costingMethod: "FIFO" | "AVCO" | "STANDARD_COST";
      standardCostRate: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }
  }
}

export const getDB = createGetDB<Namespace>();

export type DB<N extends keyof Namespace = keyof Namespace> = NamespaceDB<Namespace, N>;

export type Transaction<K extends keyof Namespace | DB = keyof Namespace> =
  NamespaceTransaction<Namespace, K>;

type TableName = NamespaceTableName<Namespace>;
export type Table<T extends TableName> = NamespaceTable<Namespace, T>;

export type Insertable<T extends TableName> = NamespaceInsertable<Namespace, T>;
export type Selectable<T extends TableName> = NamespaceSelectable<Namespace, T>;
export type Updateable<T extends TableName> = NamespaceUpdateable<Namespace, T>;
