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": {
    AllocationCycle: {
      id: Generated<string>;
      name: string;
      allocationType: "ASSESSMENT" | "DISTRIBUTION";
      companyId: string;
      accountingPeriodId: string;
      status: "DRAFT" | "VALIDATED" | "EXECUTED" | "POSTED" | "REVERSED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    AllocationResult: {
      id: Generated<string>;
      allocationCycleId: string;
      senderCostCenterId: string;
      receiverType: string;
      receiverId: string;
      allocatedAmount: number;
      costElementId: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    AllocationRule: {
      id: Generated<string>;
      allocationCycleId: string;
      senderCostCenterId: string;
      receiverType: string;
      receiverId: string;
      percentage: number;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    Budget: {
      id: Generated<string>;
      name: string;
      description: string | null;
      budgetHolderId: string;
      fiscalYearId: string;
      companyId: string;
      costObjectType: string;
      costObjectId: string;
      planVersionId: string;
      totalAmount: number;
      rejectionReason: string | null;
      status: "DRAFT" | "SUBMITTED" | "APPROVED" | "ACTIVE" | "CLOSED";
      createdByUserId: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    BudgetLineItem: {
      id: Generated<string>;
      budgetId: string;
      costElementId: string;
      accountingPeriodId: string;
      glAccountId: string | null;
      plannedAmount: number;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    Commitment: {
      id: Generated<string>;
      costElementId: string;
      costObjectType: string;
      costObjectId: string;
      accountingPeriodId: string;
      amount: number;
      consumedAmount: number;
      purchaseOrderId: string;
      companyId: string;
      status: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    CostCenter: {
      id: Generated<string>;
      code: string;
      name: string;
      description: string | null;
      category: string;
      responsiblePersonId: string | null;
      departmentId: string | null;
      companyId: string;
      hierarchyNodeId: string;
      validFrom: Timestamp;
      validTo: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    CostCenterHierarchy: {
      id: Generated<string>;
      name: string;
      version: string;
      companyId: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    CostCenterHierarchyNode: {
      id: Generated<string>;
      name: string;
      hierarchyId: string;
      parentNodeId: string | null;
      sortOrder: number;
      level: number;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    CostElement: {
      id: Generated<string>;
      code: string;
      name: string;
      type: "PRIMARY" | "SECONDARY";
      category: string;
      description: string | null;
      glAccountId: string | null;
      companyId: string;
      validFrom: Timestamp | null;
      validTo: Timestamp | null;
      status: "DRAFT" | "ACTIVE" | "INACTIVE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    InternalOrder: {
      id: Generated<string>;
      orderNumber: string;
      orderType: "OVERHEAD" | "INVESTMENT" | "ACCRUAL";
      description: string;
      responsiblePersonId: string;
      costCenterId: string;
      companyId: string;
      validFrom: Timestamp | null;
      validTo: Timestamp | null;
      status: "CREATED" | "RELEASED" | "TECHNICALLY_COMPLETED" | "CLOSED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PlanVersion: {
      id: Generated<string>;
      name: string;
      type: "BUDGET" | "FORECAST" | "SIMULATION";
      baseVersionId: string | null;
      companyId: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ProfitCenter: {
      id: Generated<string>;
      code: string;
      name: string;
      description: string | null;
      responsiblePersonId: string | null;
      companyId: string;
      validFrom: Timestamp;
      validTo: Timestamp | null;
      status: "DRAFT" | "ACTIVE" | "INACTIVE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ProfitCenterHierarchy: {
      id: Generated<string>;
      name: string;
      companyId: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ProfitCenterHierarchyAssignment: {
      id: Generated<string>;
      profitCenterId: string;
      hierarchyId: string;
      hierarchyNodeId: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ProfitCenterHierarchyNode: {
      id: Generated<string>;
      name: string;
      hierarchyId: string;
      parentNodeId: string | null;
      sortOrder: number;
      level: number;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    VarianceReport: {
      id: Generated<string>;
      costObjectType: string;
      costObjectId: string;
      accountingPeriodId: string;
      companyId: string;
      totalPlanned: number;
      totalActual: number;
      totalVariance: number;
      status: "DRAFT" | "FINAL";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    VarianceReportLine: {
      id: Generated<string>;
      varianceReportId: string;
      costElementId: string;
      accountingPeriodId: string;
      plannedAmount: number;
      actualAmount: number;
      varianceAmount: number;
      variancePercentage: number;
      varianceDirection: "FAVORABLE" | "UNFAVORABLE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

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

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

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

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

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

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

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