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" | "FUTURE_ENTERABLE" | "OPEN" | "CLOSED" | "PERMANENTLY_CLOSED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    FinancialStatement: {
      id: Generated<string>;
      companyId: string;
      chartOfAccountsId: string;
      trialBalanceId: string;
      comparativePeriodTrialBalanceId: string | null;
      statementType: "BALANCE_SHEET" | "PROFIT_AND_LOSS" | "CASH_FLOW";
      status: "DRAFT" | "FINALIZED";
      reportingDate: Timestamp | null;
      startDate: Timestamp | null;
      endDate: Timestamp | null;
      cashFlowMethod: string | null;
      generatedAt: Timestamp;
      finalizedAt: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    FinancialStatementLineItem: {
      id: Generated<string>;
      financialStatementId: string;
      accountId: string | null;
      accountGroupId: string | null;
      label: string;
      amount: number;
      comparativeAmount: number | null;
      lineType: "DETAIL" | "SUBTOTAL" | "TOTAL";
      sectionType: string;
      sortOrder: number;
      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;
      journalType: "SALES" | "PURCHASE" | "CASH" | "BANK" | "MISCELLANEOUS" | "OPENING" | "CLOSING" | "INVENTORY" | "MANUFACTURING";
      referenceNumber: string;
      status: "DRAFT" | "POSTED" | "REVERSED";
      description: string | null;
      sourceDocumentReference: string | null;
      adjustingEntryType: "ACCRUAL" | "DEFERRAL" | "RECLASSIFICATION" | null;
      reversalOfId: string | null;
      postedAt: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    JournalLine: {
      id: Generated<string>;
      journalEntryId: string;
      accountId: string;
      debitAmount: number | null;
      creditAmount: number | null;
      description: string | null;
      currencyCode: string | null;
      exchangeRate: number | null;
      functionalDebitAmount: number | null;
      functionalCreditAmount: number | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PeriodClose: {
      id: Generated<string>;
      accountingPeriodId: string;
      status: "NOT_STARTED" | "IN_PROGRESS" | "SUBLEDGERS_VERIFIED" | "ADJUSTMENTS_POSTED" | "TRIAL_BALANCE_VERIFIED" | "UNDER_REVIEW" | "SOFT_LOCKED" | "PERMANENTLY_CLOSED";
      initiatedByUserId: string;
      completionPercentage: number;
      subledgerVerificationDetails: string | null;
      trialBalanceId: string | null;
      preliminaryStatementIds: string | null;
      approvedByUserId: string | null;
      finalizedByUserId: string | null;
      yearEndCloseExecuted: boolean;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    TrialBalance: {
      id: Generated<string>;
      companyId: string;
      accountingPeriodId: string;
      chartOfAccountsId: string;
      variant: "PRE_CLOSE" | "POST_CLOSE";
      generatedAt: Timestamp;
      totalDebits: number;
      totalCredits: number;
      isBalanced: boolean;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    TrialBalanceLine: {
      id: Generated<string>;
      trialBalanceId: string;
      accountId: string;
      accountGroupId: string | null;
      accountCode: string;
      accountName: string;
      openingBalance: number;
      periodDebits: number;
      periodCredits: number;
      closingBalance: number;
      sortOrder: number;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    Account: {
      id: Generated<string>;
      code: string | null;
      name: string | null;
      classification: string | null;
      status: string | null;
      successorAccountId: string | null;
    }

    AccountGroup: {
      id: Generated<string>;
      name: string | null;
      code: string | null;
    }

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

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

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

    User: {
      id: Generated<string>;
      status: string | 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>;
