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;
    }

    InventoryLedger: {
      id: Generated<string>;
      sourceType: "GOODS_RECEIPT" | "GOODS_ISSUE" | "BLOCK" | "UNBLOCK" | "CORRECTION" | "SCRAP";
      sourceId: string;
      dcIndicator: "DR" | "CR";
      itemId: string;
      storageLocationId: string;
      quantity: string;
      amount: string | null;
      executedAt: Timestamp;
      lotId: string | null;
      serialNumberId: string | null;
      createdAt: Generated<Timestamp>;
    }

    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;
      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;
      onHand: string;
      reserved: string;
      incoming: string;
      blocked: string;
      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;
    }

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

    Item: {
      id: Generated<string>;
    }

    Site: {
      id: Generated<string>;
    }
  }
}

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>;
