import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddKnowledgeBaseEnhancements1737800000000 implements MigrationInterface {
  name = 'AddKnowledgeBaseEnhancements1737800000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Create knowledge base group table
    await queryRunner.query(`
      CREATE TABLE "knowledge_groups" (
        "id" varchar PRIMARY KEY NOT NULL,
        "name" varchar NOT NULL,
        "description" varchar,
        "color" varchar NOT NULL DEFAULT '#3B82F6',
        "user_id" varchar NOT NULL,
        "created_at" datetime NOT NULL DEFAULT (datetime('now')),
        "updated_at" datetime NOT NULL DEFAULT (datetime('now'))
      )
    `);

    // Create document group related tables
    await queryRunner.query(`
      CREATE TABLE "knowledge_base_groups" (
        "knowledge_base_id" varchar NOT NULL,
        "group_id" varchar NOT NULL,
        "created_at" datetime NOT NULL DEFAULT (datetime('now')),
        PRIMARY KEY ("knowledge_base_id", "group_id"),
        FOREIGN KEY ("knowledge_base_id") REFERENCES "knowledge_base" ("id") ON DELETE CASCADE,
        FOREIGN KEY ("group_id") REFERENCES "knowledge_groups" ("id") ON DELETE CASCADE
      )
    `);

    // Create search history table
    await queryRunner.query(`
      CREATE TABLE "search_history" (
        "id" varchar PRIMARY KEY NOT NULL,
        "user_id" varchar NOT NULL,
        "title" varchar NOT NULL,
        "selected_groups" text,
        "created_at" datetime NOT NULL DEFAULT (datetime('now')),
        "updated_at" datetime NOT NULL DEFAULT (datetime('now'))
      )
    `);

    // Create conversation message table
    await queryRunner.query(`
      CREATE TABLE "chat_messages" (
        "id" varchar PRIMARY KEY NOT NULL,
        "search_history_id" varchar NOT NULL,
        "role" varchar NOT NULL CHECK ("role" IN ('user', 'assistant')),
        "content" text NOT NULL,
        "sources" text,
        "created_at" datetime NOT NULL DEFAULT (datetime('now')),
        FOREIGN KEY ("search_history_id") REFERENCES "search_history" ("id") ON DELETE CASCADE
      )
    `);

    // Add pdf_path field to knowledge_base table
    await queryRunner.query(`
      ALTER TABLE "knowledge_base" ADD COLUMN "pdf_path" varchar
    `);

    // Create index
    await queryRunner.query(`CREATE INDEX "IDX_knowledge_groups_user_id" ON "knowledge_groups" ("user_id")`);
    await queryRunner.query(`CREATE INDEX "IDX_search_history_user_id" ON "search_history" ("user_id")`);
    await queryRunner.query(`CREATE INDEX "IDX_chat_messages_search_history_id" ON "chat_messages" ("search_history_id")`);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Delete index
    await queryRunner.query(`DROP INDEX "IDX_chat_messages_search_history_id"`);
    await queryRunner.query(`DROP INDEX "IDX_search_history_user_id"`);
    await queryRunner.query(`DROP INDEX "IDX_knowledge_groups_user_id"`);

    // Delete pdf_path field
    await queryRunner.query(`ALTER TABLE "knowledge_base" DROP COLUMN "pdf_path"`);

    // Delete table
    await queryRunner.query(`DROP TABLE "chat_messages"`);
    await queryRunner.query(`DROP TABLE "search_history"`);
    await queryRunner.query(`DROP TABLE "knowledge_base_groups"`);
    await queryRunner.query(`DROP TABLE "knowledge_groups"`);
  }
}