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": {
    Account: {
      id: Generated<string>;
      code: string;
      name: string;
      accountType: "ASSET" | "LIABILITY" | "EQUITY" | "REVENUE" | "EXPENSE";
      classification: "REGULAR" | "RECEIVABLE" | "PAYABLE" | "BANK_CASH" | "FIXED_ASSET" | "RETAINED_EARNINGS" | "NON_POSTING";
      chartOfAccountsId: string;
      accountGroupId: string | null;
      currencyRestriction: string | null;
      reconciliation: boolean;
      defaultTaxCode: string | null;
      tags: string | null;
      successorAccountId: string | null;
      hasPostedTransactions: boolean;
      status: "DRAFT" | "ACTIVE" | "INACTIVE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    AccountGroup: {
      id: Generated<string>;
      code: string;
      name: string;
      chartOfAccountsId: string;
      parentAccountGroupId: string | null;
      sortOrder: number | null;
      numberRangeStart: number | null;
      numberRangeEnd: number | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ChartOfAccounts: {
      id: Generated<string>;
      name: string;
      description: string | null;
      companyId: string;
      baseCurrencyId: string;
      status: "DRAFT" | "ACTIVE" | "ARCHIVED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    Company: {
      id: Generated<string>;
    }

    Currency: {
      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>;
