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;
      stockMovementId: string;
      quantity: number;
      unitCost: number;
      remainingQuantity: number;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    InventoryAdjustment: {
      id: Generated<string>;
      adjustmentType: "CORRECTION" | "SCRAP";
      itemId: string;
      storageLocationId: string;
      quantityDelta: number;
      reasonCode: string;
      status: "DRAFT" | "PENDING_APPROVAL" | "APPROVED" | "REJECTED" | "CONFIRMED";
      inventoryCountId: string | null;
      lotId: string | null;
      serialNumberId: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    InventoryCount: {
      id: Generated<string>;
      countType: "FULL" | "CYCLE";
      warehouseId: string | null;
      storageLocationId: string | null;
      plannedDate: Timestamp;
      status: "PLANNED" | "IN_PROGRESS" | "COMPLETED" | "CANCELLED";
      isFrozen: boolean;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    InventoryCountLine: {
      id: Generated<string>;
      inventoryCountId: string;
      itemId: string;
      storageLocationId: string;
      systemQuantity: number;
      countedQuantity: number | null;
      variance: number | null;
      status: "PENDING" | "APPROVED" | "REJECTED" | "RECOUNT";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ItemValuation: {
      id: Generated<string>;
      itemId: string;
      valuationPolicyId: string;
      totalQuantity: number;
      totalValue: number;
      costPerUnit: number;
      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: number;
      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;
    }

    StockLevel: {
      id: Generated<string>;
      itemId: string;
      storageLocationId: string;
      onHand: number;
      reserved: number;
      incoming: number;
      blocked: number;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    StockMoveLine: {
      id: Generated<string>;
      stockMovementId: string;
      itemId: string;
      quantity: number;
      sourceLocationId: string | null;
      destinationLocationId: string | null;
      lotId: string | null;
      serialNumberId: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    StockMovement: {
      id: Generated<string>;
      movementType: "GOODS_RECEIPT" | "GOODS_ISSUE" | "INTERNAL_TRANSFER" | "BLOCK" | "UNBLOCK";
      reasonCode: string | null;
      status: "DRAFT" | "CONFIRMED" | "DONE" | "CANCELLED";
      executedAt: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    StorageLocation: {
      id: Generated<string>;
      name: string;
      code: string | null;
      warehouseId: string;
      parentId: string | null;
      locationType: "zone" | "bin";
      storageCondition: string | null;
      capacity: number | null;
      status: "ACTIVE" | "INACTIVE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

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

    Warehouse: {
      id: Generated<string>;
      name: string;
      siteId: string;
      storageCondition: string | null;
      capacity: number | null;
      status: "ACTIVE" | "INACTIVE";
      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>;
