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

export interface Namespace {
  "main-db": {
    AccountPayableDistributionLine: {
      id: Generated<string>;
      accountPayableDocumentLineId: string;
      correctionOfDistributionLineId: string | null;
      accountId: string;
      amount: string;
      description: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    AccountPayableDocument: {
      id: Generated<string>;
      companyId: string;
      businessPartnerId: string;
      currencyId: string;
      payableControlAccountId: string;
      documentType: "INVOICE" | "CREDIT_MEMO";
      correctionOfId: string | null;
      documentNumber: Serial<string>;
      externalDocumentNumber: string | null;
      documentDate: Timestamp;
      postingDate: Timestamp;
      totalAmount: string;
      description: string | null;
      status: "DRAFT" | "REGISTERED" | "CANCELLED" | "POSTED";
      registeredAt: Timestamp | null;
      postedAt: Timestamp | null;
      cancelledAt: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    AccountPayableDocumentHold: {
      id: Generated<string>;
      accountPayableDocumentId: string;
      accountPayableDocumentLineId: string | null;
      reasonCode: "INVOICE_UNIT_DIFFERS_FROM_PURCHASE_ORDER_UNIT" | "INVOICE_UNIT_PRICE_DIFFERS_FROM_PURCHASE_ORDER_UNIT_PRICE" | "INVOICE_QUANTITY_EXCEEDS_ORDERED_QUANTITY" | "INVOICE_QUANTITY_EXCEEDS_RECEIVED_QUANTITY";
      source: "SYSTEM" | "MANUAL";
      releasedAt: Timestamp | null;
      releaseReason: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    AccountPayableDocumentLine: {
      id: Generated<string>;
      accountPayableDocumentId: string;
      netAmount: string;
      taxAmount: string | null;
      grossAmount: string;
      correctionOfLineId: string | null;
      correctionType: "PRICE" | "QUANTITY" | "AMOUNT" | null;
      quantity: string | null;
      unitPrice: string | null;
      unitId: string | null;
      description: string | null;
      sourceType: "PURCHASE_ORDER" | null;
      purchaseOrderLineId: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    AccountPayableDueScheduleLine: {
      id: Generated<string>;
      accountPayableDocumentId: string;
      dueDate: Timestamp;
      amount: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    InvoiceToleranceConfig: {
      id: Generated<string>;
      companyId: string;
      supplierId: string;
      quantityAbsoluteTolerance: string | null;
      quantityPercentageTolerance: string | null;
      unitPriceAbsoluteTolerance: string | null;
      unitPricePercentageTolerance: 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>;
