import { MigrationInterface, QueryRunner } from 'typeorm';
import { Constants } from '../common/constants';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`ALTER TYPE "public"."settings_type_enum" RENAME TO "settings_type_enum_old"`);
    await queryRunner.query(`CREATE TYPE "public"."settings_type_enum" AS ENUM('facebook_followers_count', 'instagram_followers_count', 'tiktok_followers_count', 'youtube_subscriber_count', 'category_min', 'category_max', 'industry_min', 'industry_max', 'platform_charge', 'job_category_score', 'platform_score', 'geography_score', 'currency_score', 'registered_influencer_score', 'influencer_job_category_score', 'influencer_platform_score', 'influencer_geography_score', 'influencer_currency_score', 'trending_follower_count', 'trending_engagement_count', 'trending_percentage', 'encryption', 'comet_chat_message_notification', 'ai_agent_report', 'ai_model')`);
    await queryRunner.query(`ALTER TABLE "settings" ALTER COLUMN "type" DROP DEFAULT`);
    await queryRunner.query(`ALTER TABLE "settings" ALTER COLUMN "type" TYPE "public"."settings_type_enum" USING "type"::"text"::"public"."settings_type_enum"`);
    await queryRunner.query(`ALTER TABLE "settings" ALTER COLUMN "type" SET DEFAULT 'facebook_followers_count'`);
    await queryRunner.query(`DROP TYPE "public"."settings_type_enum_old"`);

    await queryRunner.query(
      `INSERT INTO settings (type, setting_type, value, name)
       SELECT $1, $2, $3, $4
       WHERE NOT EXISTS (
         SELECT 1 FROM settings WHERE type = $1 AND deleted_at IS NULL
       )`,
      ['ai_model', 'ai_prompt', Constants.AI_PROMPT.MODEL, 'AI Model'],
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DELETE FROM settings WHERE type = 'ai_model'`);

    await queryRunner.query(`CREATE TYPE "public"."settings_type_enum_old" AS ENUM('facebook_followers_count', 'instagram_followers_count', 'tiktok_followers_count', 'youtube_subscriber_count', 'category_min', 'category_max', 'industry_min', 'industry_max', 'platform_charge', 'job_category_score', 'platform_score', 'geography_score', 'currency_score', 'registered_influencer_score', 'influencer_job_category_score', 'influencer_platform_score', 'influencer_geography_score', 'influencer_currency_score', 'trending_follower_count', 'trending_engagement_count', 'trending_percentage', 'encryption', 'comet_chat_message_notification', 'ai_agent_report')`);
    await queryRunner.query(`ALTER TABLE "settings" ALTER COLUMN "type" DROP DEFAULT`);
    await queryRunner.query(`ALTER TABLE "settings" ALTER COLUMN "type" TYPE "public"."settings_type_enum_old" USING "type"::"text"::"public"."settings_type_enum_old"`);
    await queryRunner.query(`ALTER TABLE "settings" ALTER COLUMN "type" SET DEFAULT 'facebook_followers_count'`);
    await queryRunner.query(`DROP TYPE "public"."settings_type_enum"`);
    await queryRunner.query(`ALTER TYPE "public"."settings_type_enum_old" RENAME TO "settings_type_enum"`);
  }
}
