import { MigrationInterface, QueryRunner } from "typeorm";

export class AddPesTrackingAndModified1753440178783 implements MigrationInterface {
    name = 'AddPesTrackingAndModified1753440178783'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`pes_templates\` DROP FOREIGN KEY \`FK_6b0167e0b8151ad11c856a2f5cc\``);
        await queryRunner.query(`CREATE TABLE \`pes_email_events\` (\`id\` varchar(36) NOT NULL, \`type\` varchar(255) NOT NULL, \`ip_address\` varchar(255) NULL, \`user_agent\` text NULL, \`original_url\` text NULL, \`event_time\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`email_tracking_id\` varchar(36) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`pes_email_tracking\` (\`id\` varchar(36) NOT NULL, \`email\` varchar(255) NOT NULL, \`campaign_tracking_id\` varchar(255) NOT NULL, \`service_message_id\` varchar(255) NULL, \`is_opened\` tinyint NOT NULL DEFAULT 0, \`first_opened_at\` timestamp NULL, \`open_count\` int NOT NULL DEFAULT '0', \`click_count\` int NOT NULL DEFAULT '0', \`sent_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`campaign_id\` varchar(100) NULL, PRIMARY KEY (\`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 CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_email_events\` ADD CONSTRAINT \`FK_db890e957bbd8d51d92ef1fc18d\` FOREIGN KEY (\`email_tracking_id\`) REFERENCES \`pes_email_tracking\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`pes_email_tracking\` ADD CONSTRAINT \`FK_ce0edb48b0a9b4239daecb523a1\` FOREIGN KEY (\`campaign_id\`) REFERENCES \`pes_campaigns\`(\`campaign_id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`pes_email_tracking\` DROP FOREIGN KEY \`FK_ce0edb48b0a9b4239daecb523a1\``);
        await queryRunner.query(`ALTER TABLE \`pes_email_events\` DROP FOREIGN KEY \`FK_db890e957bbd8d51d92ef1fc18d\``);
        await queryRunner.query(`ALTER TABLE \`pes_templates\` DROP FOREIGN KEY \`FK_6b0167e0b8151ad11c856a2f5cc\``);
        await queryRunner.query(`DROP TABLE \`pes_email_tracking\``);
        await queryRunner.query(`DROP TABLE \`pes_email_events\``);
        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`);
    }

}
