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": {
    GoodsReceipt: {
      id: Generated<string>;
      companyId: string;
      supplierId: string;
      purchaseOrderId: string;
      receiptDate: Timestamp;
      status: "DRAFT" | "POSTED" | "CANCELLED";
      lines: string;
      inventoryHandoff: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchaseBill: {
      id: Generated<string>;
      companyId: string;
      supplierId: string;
      invoiceNumber: string;
      invoiceDate: Timestamp;
      status: "DRAFT" | "BLOCKED" | "MATCHED" | "CANCELLED";
      lines: string;
      paymentTermSnapshot: string | null;
      supplierTaxIdentification: string | null;
      matchingMode: string | null;
      varianceReason: string | null;
      releaseReason: string | null;
      dueSchedule: string | null;
      downstreamAccepted: boolean;
      comparisonEvidence: string | null;
      billReadyHandoff: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchaseOrder: {
      id: Generated<string>;
      companyId: string;
      supplierId: string;
      documentReceivingSiteId: string | null;
      paymentTermId: string | null;
      status: "DRAFT" | "SUBMITTED" | "ORDERED" | "PARTIALLY_RECEIVED" | "RECEIVED" | "PARTIALLY_BILLED" | "BILLED" | "CANCELLED" | "CLOSED";
      orderDate: Timestamp;
      externalSupplierOrderReference: string | null;
      lines: string;
      paymentTermSnapshot: string | null;
      rejectionReason: string | null;
      closeReason: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PurchasePaymentTerm: {
      id: Generated<string>;
      companyId: string;
      name: string;
      code: string | null;
      status: "ACTIVE" | "INACTIVE";
      scheduleLines: string;
      notes: 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: number;
      unitPrice: number;
      effectiveFrom: Timestamp;
      effectiveTo: Timestamp | null;
      priority: number;
      status: "ACTIVE" | "INACTIVE";
      notes: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    SupplierProfile: {
      id: Generated<string>;
      companyId: string;
      supplierId: string;
      defaultPurchasePaymentTermId: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    BusinessPartner: {
      id: Generated<string>;
      companyId: string | null;
      name: string | null;
      status: string | null;
    }

    Company: {
      id: Generated<string>;
      status: string | null;
    }

    Currency: {
      id: Generated<string>;
      code: string | null;
      isActive: boolean | null;
    }

    Item: {
      id: Generated<string>;
      unitId: string | null;
      status: string | null;
      name: string | null;
      sku: string | null;
    }

    PartnerIdentification: {
      id: Generated<string>;
      partnerId: string | null;
      type: string | null;
      value: string | null;
      validFrom: Timestamp | null;
      validTo: Timestamp | null;
    }

    PartnerRole: {
      id: Generated<string>;
      partnerId: string | null;
      role: string | null;
    }

    Site: {
      id: Generated<string>;
      companyId: string | null;
      status: string | null;
    }

    Unit: {
      id: Generated<string>;
      categoryId: string | null;
      symbol: string | null;
      isActive: boolean | 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>;
