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": {
    AccountingPeriod: {
      id: Generated<string>;
      companyId: string;
      fiscalYearId: string;
      name: string;
      startDate: Timestamp;
      endDate: Timestamp;
      periodType: "OPERATING" | "ADJUSTMENT";
      status: "NEVER_OPENED" | "OPEN" | "CLOSED" | "PERMANENTLY_CLOSED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    FiscalYear: {
      id: Generated<string>;
      companyId: string;
      name: string;
      startDate: Timestamp;
      endDate: Timestamp;
      yearEndCloseExecuted: boolean;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    JournalEntry: {
      id: Generated<string>;
      companyId: string;
      accountingPeriodId: string;
      entryDate: Timestamp;
      status: "DRAFT" | "CANCELLED" | "POSTED";
      description: string | null;
      sourceDocumentType: "ACCOUNT_PAYABLE_DOCUMENT" | "PURCHASE_BILL" | "SALES_INVOICE" | "INVENTORY_EXECUTION" | "PRODUCTION_ORDER" | "YEAR_END_CLOSE" | null;
      sourceDocumentId: string | null;
      reversalOfId: string | null;
      postedAt: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    JournalLine: {
      id: Generated<string>;
      journalEntryId: string;
      accountId: string;
      debitAmount: string | null;
      creditAmount: string | null;
      description: 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>;
