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": {
    AuditEntry: {
      id: Generated<string>;
      eventId: string;
      actorType: string;
      actorId: string;
      entityType: string;
      entityId: string;
      operationType: string;
      companyId: string | null;
      correlationId: string | null;
      ipAddress: string | null;
      userAgent: string | null;
      sessionId: string | null;
      requestId: string | null;
      onBehalfOf: string | null;
      timestamp: Timestamp;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    AuditPolicy: {
      id: Generated<string>;
      entityName: string;
      companyId: string | null;
      operationType: string;
      status: "DRAFT" | "ACTIVE" | "INACTIVE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    AuditableEntity: {
      id: Generated<string>;
      entityName: string;
      entityScope: string;
      auditableFields: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ChangeDetail: {
      id: Generated<string>;
      auditEntryId: string;
      fieldName: string;
      oldValue: string | null;
      newValue: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PolicyFieldRule: {
      id: Generated<string>;
      auditPolicyId: string;
      fieldName: string;
      sensitivityMode: string;
      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>;
