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": {
    ApprovalDecision: {
      id: Generated<string>;
      approvalRequestId: string;
      approvalStepId: string | null;
      decision: "APPROVE" | "REJECT" | "SEND_BACK" | "DELEGATE" | "WITHDRAW" | "CANCEL" | "RESUBMIT";
      decidedByUserId: string;
      delegatedToUserId: string | null;
      comment: string | null;
      decidedAt: Timestamp;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ApprovalPolicy: {
      id: Generated<string>;
      purpose: string;
      name: string;
      description: string | null;
      status: "DRAFT" | "ACTIVE" | "INACTIVE";
      activatedAt: Timestamp | null;
      deactivatedAt: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ApprovalPolicyStep: {
      id: Generated<string>;
      approvalPolicyId: string;
      stepOrder: number;
      name: string;
      minimumApprovals: number;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ApprovalPolicyStepAssignee: {
      id: Generated<string>;
      approvalPolicyStepId: string;
      userId: string | null;
      roleId: string | null;
      required: boolean;
      roleQuorum: "ALL" | "ANY" | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ApprovalRequest: {
      id: Generated<string>;
      name: string;
      purpose: string;
      targetEntityType: string;
      targetEntityId: string;
      requesterId: string;
      sourcePolicyId: string | null;
      status: "PENDING" | "APPROVED" | "REJECTED" | "REVISION_REQUESTED" | "WITHDRAWN" | "CANCELLED";
      rejectionReason: string | null;
      resolvedAt: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ApprovalStep: {
      id: Generated<string>;
      approvalRequestId: string;
      stepOrder: number;
      name: string;
      minimumApprovals: number;
      status: "PENDING" | "IN_PROGRESS" | "APPROVED" | "REJECTED";
      resolvedAt: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ApprovalStepAssignee: {
      id: Generated<string>;
      approvalStepId: string;
      userId: string | null;
      roleId: string | null;
      required: boolean;
      roleQuorum: "ALL" | "ANY" | null;
      status: "PENDING" | "APPROVED" | "REJECTED" | "DELEGATED";
      resolvedAt: Timestamp | 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>;
