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": {
    Pipeline: {
      id: Generated<string>;
      name: string;
      description: string | null;
      pipelineType: string;
      createdByUserId: string;
      status: "ACTIVE" | "ARCHIVED";
      locked: boolean;
      itemNumberSeq: number | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PipelineItem: {
      id: Generated<string>;
      pipelineId: string;
      stageId: string;
      title: string;
      description: string | null;
      assigneeId: string | null;
      priority: "LOW" | "MEDIUM" | "HIGH" | "URGENT";
      dueDate: Timestamp | null;
      position: number;
      itemNumber: number;
      lifecycle: "DRAFT" | "OPEN" | "CLOSED";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PipelineItemChange: {
      id: Generated<string>;
      itemId: string;
      changedByUserId: string;
      changeType: "CONTENT" | "FIELD" | "LIFECYCLE" | "LABEL";
      fieldName: "TITLE" | "DESCRIPTION" | "ASSIGNEE" | "PRIORITY" | "DUE_DATE" | "LIFECYCLE" | "LABEL";
      prevValue: string | null;
      newValue: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PipelineItemComment: {
      id: Generated<string>;
      itemId: string;
      authorUserId: string;
      body: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PipelineItemLabel: {
      id: Generated<string>;
      itemId: string;
      labelId: string;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PipelineLabel: {
      id: Generated<string>;
      pipelineId: string;
      name: string;
      color: string | null;
      description: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PipelineStage: {
      id: Generated<string>;
      pipelineId: string;
      name: string;
      position: number;
      category: "NOT_STARTED" | "IN_PROGRESS" | "DONE" | "CANCELED";
      color: string | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PipelineStageTransition: {
      id: Generated<string>;
      itemId: string;
      fromStageId: string;
      toStageId: string;
      movedByUserId: 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>;
