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": {
    PurchaseOrder: {
      id: Generated<string>;
      companyId: string;
      supplierId: string;
      receivingSiteId: string | null;
      orderStatus: "DRAFT" | "SUBMITTED" | "ORDERED" | "CANCELLED" | "CLOSED";
      receiptStatus: "NOT_RECEIVED" | "PARTIALLY_RECEIVED" | "RECEIVED" | null;
      billingStatus: "NOT_BILLED" | "PARTIALLY_BILLED" | "BILLED" | null;
      orderDate: Timestamp;
      externalSupplierOrderReference: string | null;
      supplierSnapshotName: string | null;
      rejectionReason: string | null;
      closeReason: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchaseOrderFieldChange: {
      id: Generated<string>;
      revisionId: string;
      recordType: "HEADER" | "LINE";
      recordId: string;
      fieldName: string;
      changeKind: "ADDED" | "MODIFIED" | "REMOVED";
      oldValue: string | null;
      newValue: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchaseOrderLine: {
      id: Generated<string>;
      purchaseOrderId: string;
      itemId: string;
      itemSnapshotName: string | null;
      itemSnapshotSku: string | null;
      quantity: string;
      unitPrice: string;
      unitId: string;
      receivingSiteId: string | null;
      requiresPhysicalReceipt: boolean;
      billedQuantity: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchaseOrderRevision: {
      id: Generated<string>;
      purchaseOrderId: string;
      revisionNumber: number;
      reason: string | null;
      amendedByUserId: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchasePriceList: {
      id: Generated<string>;
      companyId: string;
      supplierId: string;
      name: string;
      code: string | null;
      priority: number;
      status: "ACTIVE" | "INACTIVE";
      notes: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchasePriceRule: {
      id: Generated<string>;
      priceListId: string;
      itemId: string;
      currencyId: string;
      unitId: string;
      minimumQuantity: string;
      unitPrice: string;
      effectiveFrom: Timestamp;
      effectiveTo: Timestamp | null;
      priority: number;
      status: "ACTIVE" | "INACTIVE";
      notes: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchaseRequisition: {
      id: Generated<string>;
      companyId: string;
      requesterId: string;
      status: "DRAFT" | "SUBMITTED" | "APPROVED" | "CONVERTED" | "CLOSED" | "CANCELLED";
      justification: string | null;
      desiredDeliveryDate: Timestamp | null;
      rejectionReason: string | null;
      closeReason: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchaseRequisitionLine: {
      id: Generated<string>;
      purchaseRequisitionId: string;
      description: string;
      quantity: string;
      estimatedUnitPrice: string;
      itemId: string;
      unitId: string;
      suggestedSupplierId: string | null;
      needByDate: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchaseRequisitionLineAllocation: {
      id: Generated<string>;
      purchaseRequisitionId: string;
      purchaseRequisitionLineId: string;
      purchaseOrderId: string;
      purchaseOrderLineId: string;
      allocatedQuantity: string;
      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>;
