import { MigrationInterface, QueryRunner } from "typeorm";

export class AddPesEntity1753274621461 implements MigrationInterface {
    name = 'AddPesEntity1753274621461'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE \`pes_templates\` (\`id\` varchar(36) NOT NULL, \`template_id\` varchar(100) NOT NULL, \`name\` varchar(255) NOT NULL, \`description\` text NOT NULL, \`category\` enum ('lottery', 'banking', 'social_media', 'shopping', 'government', 'tech_support', 'romantic', 'employment', 'cryptocurrency', 'healthcare', 'educational', 'news', 'security_alert') NOT NULL, \`difficulty_level\` enum ('beginner', 'intermediate', 'advanced', 'expert') NOT NULL DEFAULT 'beginner', \`subject\` varchar(255) NOT NULL, \`html_content\` text NOT NULL, \`text_content\` text NOT NULL, \`red_flags\` json NULL, \`is_system_template\` tinyint NOT NULL DEFAULT 0, \`is_active\` tinyint NOT NULL DEFAULT 1, \`usage_count\` int NOT NULL DEFAULT '0', \`success_rate\` decimal(5,2) NOT NULL DEFAULT '0.00', \`created_by\` varchar(255) NOT NULL, \`notes\` text NULL, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), UNIQUE INDEX \`IDX_90595b9c7d250820ebe7785959\` (\`template_id\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`pes_sending_profiles\` (\`id\` varchar(36) NOT NULL, \`profile_id\` varchar(100) NOT NULL, \`name\` varchar(255) NOT NULL, \`description\` text NULL, \`from_address\` varchar(255) NOT NULL, \`from_name\` varchar(255) NULL, \`profile_type\` enum ('smtp', 'api', 'system_default') NOT NULL DEFAULT 'system_default', \`smtp_host\` varchar(255) NULL, \`smtp_port\` int NULL, \`smtp_username\` varchar(255) NULL, \`smtp_password\` text NULL, \`smtp_tls\` tinyint NOT NULL DEFAULT 0, \`verified_user_id\` varchar(255) NOT NULL, \`is_active\` tinyint NOT NULL DEFAULT 1, \`is_default\` tinyint NOT NULL DEFAULT 0, \`usage_count\` int NOT NULL DEFAULT '0', \`last_used_at\` timestamp NULL, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`verified_user\` varchar(36) NULL, UNIQUE INDEX \`IDX_85198756410e141f8ddbbeb727\` (\`profile_id\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`pes_landing_pages\` (\`id\` varchar(36) NOT NULL, \`page_id\` varchar(100) NOT NULL, \`name\` varchar(255) NOT NULL, \`description\` text NULL, \`html_content\` text NULL, \`capture_credentials\` tinyint NOT NULL DEFAULT 0, \`capture_passwords\` tinyint NOT NULL DEFAULT 0, \`redirect_url\` varchar(255) NULL, \`verified_user_id\` varchar(255) NULL, \`is_active\` tinyint NOT NULL DEFAULT 1, \`visit_count\` int NOT NULL DEFAULT '0', \`submission_count\` int NOT NULL DEFAULT '0', \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`user_id\` varchar(36) NULL, UNIQUE INDEX \`IDX_1f607a3c35a28e1c815299548b\` (\`page_id\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`pes_recipients\` (\`id\` varchar(36) NOT NULL, \`email\` varchar(255) NOT NULL, \`first_name\` varchar(255) NULL, \`last_name\` varchar(255) NULL, \`department\` varchar(100) NULL, \`is_verified\` tinyint NOT NULL DEFAULT 0, \`notes\` text NULL, \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), UNIQUE INDEX \`IDX_ebaa9ff853aa5b8d90cc4b3b42\` (\`email\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`pes_recipient_groups\` (\`id\` varchar(36) NOT NULL, \`name\` varchar(255) NOT NULL, \`description\` text NULL, \`department\` varchar(100) NULL, \`verified_user_id\` varchar(255) NOT NULL, \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`pes_campaigns\` (\`id\` varchar(36) NOT NULL, \`campaign_id\` varchar(100) NOT NULL, \`name\` varchar(255) NOT NULL, \`description\` text NULL, \`status\` enum ('draft', 'scheduled', 'running', 'completed', 'paused', 'cancelled') NOT NULL DEFAULT 'draft', \`scheduled_date\` timestamp NULL, \`launch_date\` timestamp NULL, \`completed_date\` timestamp NULL, \`tracking_id\` varchar(255) NULL, \`send_immediately\` tinyint NOT NULL DEFAULT 0, \`notes\` text NULL, \`recipient_group_id\` varchar(255) NULL, \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`verified_user_id\` varchar(36) NULL, \`template_id\` varchar(36) NULL, \`sending_profile_id\` varchar(36) NULL, \`landing_page_id\` varchar(36) NULL, UNIQUE INDEX \`IDX_dccbebe604037e4e974eaf0525\` (\`campaign_id\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`pes_verified_users\` (\`id\` varchar(36) NOT NULL, \`email\` varchar(255) NOT NULL, \`user_id\` int NOT NULL, \`daily_email_count\` int NOT NULL DEFAULT '0', \`total_email_count\` int NOT NULL DEFAULT '0', \`total_campaigns\` int NOT NULL DEFAULT '0', \`last_email_date\` timestamp NULL, \`is_active\` tinyint NOT NULL DEFAULT 1, \`block_reason\` text NULL, \`blocked_at\` timestamp NULL, \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), UNIQUE INDEX \`IDX_d61853d5308975d5177fe4a762\` (\`email\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`pes_email_verification\` (\`id\` varchar(36) NOT NULL, \`verification_id\` varchar(255) NOT NULL, \`email\` varchar(255) NOT NULL, \`method\` enum ('otp', 'gmail_oauth') NOT NULL DEFAULT 'otp', \`status\` enum ('pending', 'verified', 'expired', 'failed') NOT NULL DEFAULT 'pending', \`otp_code\` varchar(6) NULL, \`otp_expires_at\` timestamp NULL, \`verified_at\` timestamp NULL, \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), UNIQUE INDEX \`IDX_4d09f42682ab78f24f976e721e\` (\`verification_id\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`pes_recipient_group_mappings\` (\`group_id\` varchar(36) NOT NULL, \`recipient_id\` varchar(36) NOT NULL, INDEX \`IDX_08b84416e2789882de01de5d6b\` (\`group_id\`), INDEX \`IDX_1095c795ed76ad33a79153343a\` (\`recipient_id\`), PRIMARY KEY (\`group_id\`, \`recipient_id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`ALTER TABLE \`pes_templates\` ADD CONSTRAINT \`FK_6b0167e0b8151ad11c856a2f5cc\` FOREIGN KEY (\`created_by\`) REFERENCES \`pes_verified_users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_sending_profiles\` ADD CONSTRAINT \`FK_56b31a69aa29df321896abbdf59\` FOREIGN KEY (\`verified_user\`) REFERENCES \`pes_verified_users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_landing_pages\` ADD CONSTRAINT \`FK_f4cbc838be15ab344c19917a062\` FOREIGN KEY (\`user_id\`) REFERENCES \`pes_verified_users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_recipient_groups\` ADD CONSTRAINT \`FK_3db0319a09c99cb6fffbfcd55fa\` FOREIGN KEY (\`verified_user_id\`) REFERENCES \`pes_verified_users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_campaigns\` ADD CONSTRAINT \`FK_2d69c95012ce34fb2b433231ee4\` FOREIGN KEY (\`verified_user_id\`) REFERENCES \`pes_verified_users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_campaigns\` ADD CONSTRAINT \`FK_1ead0fd821b4aa921fc2ef499fe\` FOREIGN KEY (\`template_id\`) REFERENCES \`pes_templates\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_campaigns\` ADD CONSTRAINT \`FK_d0d2ec97f74ec2a94efa7aacc4d\` FOREIGN KEY (\`sending_profile_id\`) REFERENCES \`pes_sending_profiles\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_campaigns\` ADD CONSTRAINT \`FK_588afdca1868e4abccfe9450501\` FOREIGN KEY (\`landing_page_id\`) REFERENCES \`pes_landing_pages\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_campaigns\` ADD CONSTRAINT \`FK_4159382d994da167f1cfba5d387\` FOREIGN KEY (\`recipient_group_id\`) REFERENCES \`pes_recipient_groups\`(\`id\`) ON DELETE SET NULL ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_recipient_group_mappings\` ADD CONSTRAINT \`FK_08b84416e2789882de01de5d6b2\` FOREIGN KEY (\`group_id\`) REFERENCES \`pes_recipient_groups\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`);
        await queryRunner.query(`ALTER TABLE \`pes_recipient_group_mappings\` ADD CONSTRAINT \`FK_1095c795ed76ad33a79153343ac\` FOREIGN KEY (\`recipient_id\`) REFERENCES \`pes_recipients\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`pes_recipient_group_mappings\` DROP FOREIGN KEY \`FK_1095c795ed76ad33a79153343ac\``);
        await queryRunner.query(`ALTER TABLE \`pes_recipient_group_mappings\` DROP FOREIGN KEY \`FK_08b84416e2789882de01de5d6b2\``);
        await queryRunner.query(`ALTER TABLE \`pes_campaigns\` DROP FOREIGN KEY \`FK_4159382d994da167f1cfba5d387\``);
        await queryRunner.query(`ALTER TABLE \`pes_campaigns\` DROP FOREIGN KEY \`FK_588afdca1868e4abccfe9450501\``);
        await queryRunner.query(`ALTER TABLE \`pes_campaigns\` DROP FOREIGN KEY \`FK_d0d2ec97f74ec2a94efa7aacc4d\``);
        await queryRunner.query(`ALTER TABLE \`pes_campaigns\` DROP FOREIGN KEY \`FK_1ead0fd821b4aa921fc2ef499fe\``);
        await queryRunner.query(`ALTER TABLE \`pes_campaigns\` DROP FOREIGN KEY \`FK_2d69c95012ce34fb2b433231ee4\``);
        await queryRunner.query(`ALTER TABLE \`pes_recipient_groups\` DROP FOREIGN KEY \`FK_3db0319a09c99cb6fffbfcd55fa\``);
        await queryRunner.query(`ALTER TABLE \`pes_landing_pages\` DROP FOREIGN KEY \`FK_f4cbc838be15ab344c19917a062\``);
        await queryRunner.query(`ALTER TABLE \`pes_sending_profiles\` DROP FOREIGN KEY \`FK_56b31a69aa29df321896abbdf59\``);
        await queryRunner.query(`ALTER TABLE \`pes_templates\` DROP FOREIGN KEY \`FK_6b0167e0b8151ad11c856a2f5cc\``);
        await queryRunner.query(`DROP INDEX \`IDX_1095c795ed76ad33a79153343a\` ON \`pes_recipient_group_mappings\``);
        await queryRunner.query(`DROP INDEX \`IDX_08b84416e2789882de01de5d6b\` ON \`pes_recipient_group_mappings\``);
        await queryRunner.query(`DROP TABLE \`pes_recipient_group_mappings\``);
        await queryRunner.query(`DROP INDEX \`IDX_4d09f42682ab78f24f976e721e\` ON \`pes_email_verification\``);
        await queryRunner.query(`DROP TABLE \`pes_email_verification\``);
        await queryRunner.query(`DROP INDEX \`IDX_d61853d5308975d5177fe4a762\` ON \`pes_verified_users\``);
        await queryRunner.query(`DROP TABLE \`pes_verified_users\``);
        await queryRunner.query(`DROP INDEX \`IDX_dccbebe604037e4e974eaf0525\` ON \`pes_campaigns\``);
        await queryRunner.query(`DROP TABLE \`pes_campaigns\``);
        await queryRunner.query(`DROP TABLE \`pes_recipient_groups\``);
        await queryRunner.query(`DROP INDEX \`IDX_ebaa9ff853aa5b8d90cc4b3b42\` ON \`pes_recipients\``);
        await queryRunner.query(`DROP TABLE \`pes_recipients\``);
        await queryRunner.query(`DROP INDEX \`IDX_1f607a3c35a28e1c815299548b\` ON \`pes_landing_pages\``);
        await queryRunner.query(`DROP TABLE \`pes_landing_pages\``);
        await queryRunner.query(`DROP INDEX \`IDX_85198756410e141f8ddbbeb727\` ON \`pes_sending_profiles\``);
        await queryRunner.query(`DROP TABLE \`pes_sending_profiles\``);
        await queryRunner.query(`DROP INDEX \`IDX_90595b9c7d250820ebe7785959\` ON \`pes_templates\``);
        await queryRunner.query(`DROP TABLE \`pes_templates\``);
    }

}
