import { MigrationInterface, QueryRunner } from "typeorm";

export class First1742405828705 implements MigrationInterface {
    name = 'First1742405828705'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE \`comments\` (\`id\` int NOT NULL AUTO_INCREMENT, \`content\` text NOT NULL, \`author_id\` int NOT NULL, \`attachment_url\` varchar(255) NULL, \`ticket_id\` int NOT 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), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`tickets\` (\`id\` int NOT NULL AUTO_INCREMENT, \`title\` varchar(255) NOT NULL, \`description\` text NOT NULL, \`status\` enum ('open', 'in_progress', 'review', 'closed') NOT NULL DEFAULT 'open', \`priority\` enum ('low', 'medium', 'high', 'critical') NOT NULL DEFAULT 'medium', \`author_id\` int NOT NULL, \`assignee_id\` int NULL, \`attachment_url\` varchar(255) NULL, \`labels\` 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), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`api_keys\` (\`id\` varchar(36) NOT NULL, \`key\` varchar(255) NOT NULL, \`name\` varchar(255) NOT NULL, \`permissions\` text NOT NULL, \`is_active\` tinyint NOT NULL DEFAULT 1, \`user_id\` int NOT NULL, \`rate_limit\` int NOT NULL COMMENT 'Custom rate limit for this API key' DEFAULT '100', \`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), \`last_used_at\` timestamp NULL, \`usage_count\` int NOT NULL DEFAULT '0', \`tier\` enum ('free', 'business') NOT NULL DEFAULT 'free', \`minute_limit\` int NOT NULL DEFAULT '100', \`daily_limit\` int NOT NULL DEFAULT '1000', \`monthly_limit\` int NOT NULL DEFAULT '10000', \`minute_usage\` int NOT NULL DEFAULT '0', \`daily_usage\` int NOT NULL DEFAULT '0', \`monthly_usage\` int NOT NULL DEFAULT '0', \`last_minute_reset\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, \`last_daily_reset\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, \`last_monthly_reset\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE INDEX \`IDX_e42cf55faeafdcce01a82d2484\` (\`key\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`users\` (\`id\` int NOT NULL AUTO_INCREMENT, \`first_name\` varchar(60) NOT NULL, \`last_name\` varchar(60) NOT NULL, \`email\` varchar(100) NOT NULL, \`password\` varchar(60) NOT NULL, \`role\` enum ('super_admin', 'admin', 'user', 'audience') NOT NULL DEFAULT 'user', \`referral_mail\` varchar(255) NULL, \`picture\` varchar(255) NULL DEFAULT 'https://cdn-icons-png.flaticon.com/512/149/149071.png', \`google_id\` varchar(255) NULL, \`is_google_user\` tinyint 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_97672ac88f789774dd47f7c8be\` (\`email\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`roles\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(255) NOT NULL, UNIQUE INDEX \`IDX_648e3f5447f725579d7d4ffdfb\` (\`name\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`permissions\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(255) NOT NULL, UNIQUE INDEX \`IDX_48ce552495d14eae9b187bb671\` (\`name\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`question_responses\` (\`id\` int NOT NULL AUTO_INCREMENT, \`question_id\` varchar(255) NOT NULL, \`question_text\` varchar(255) NOT NULL, \`answer\` varchar(255) NOT NULL, \`report_id\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`reports\` (\`id\` int NOT NULL AUTO_INCREMENT, \`gender\` varchar(255) NOT NULL, \`age\` varchar(255) NOT NULL, \`state\` varchar(255) NOT NULL, \`district\` varchar(255) NOT NULL, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`subscribers\` (\`id\` int NOT NULL AUTO_INCREMENT, \`email\` varchar(255) NOT NULL, \`is_active\` tinyint NOT NULL DEFAULT 1, \`host_type\` enum ('can', 'dfuk') NOT NULL DEFAULT 'dfuk', UNIQUE INDEX \`IDX_1a7163c08f0e57bd1c9821508b\` (\`email\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`newsletter_logs\` (\`id\` int NOT NULL AUTO_INCREMENT, \`send_to\` enum ('can', 'dfuk', 'all', 'custom') NOT NULL, \`subject\` varchar(255) NOT NULL, \`body\` text NOT NULL, \`email_count\` int NOT NULL, \`sent_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`blog_comments\` (\`comment_id\` int NOT NULL AUTO_INCREMENT, \`content\` text NOT NULL, \`user_id\` int NOT NULL, \`blog_id\` int NOT 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), PRIMARY KEY (\`comment_id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`blogs\` (\`blog_id\` int NOT NULL AUTO_INCREMENT, \`is_published\` tinyint NOT NULL DEFAULT 0, \`tags\` text NULL, \`featured_image_url\` varchar(255) NULL, \`view_count\` int NOT NULL DEFAULT '0', \`author_names\` text NOT NULL, \`slug\` varchar(255) NOT 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), PRIMARY KEY (\`blog_id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`blog_translations\` (\`translation_id\` int NOT NULL AUTO_INCREMENT, \`blog_id\` int NOT NULL, \`language\` varchar(2) NOT NULL, \`title\` varchar(255) NOT NULL, \`content\` text NOT NULL, PRIMARY KEY (\`translation_id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`alerts\` (\`id\` int NOT NULL AUTO_INCREMENT, \`title\` varchar(255) NOT NULL, \`description\` text NOT NULL, \`source\` varchar(255) NOT NULL, \`source_url\` varchar(255) NULL, \`attack_time\` timestamp NULL, \`target_country\` varchar(255) NOT NULL, \`target_organization\` varchar(255) NOT NULL, \`attacker_info\` varchar(255) NULL, \`attack_type\` enum ('ddos', 'phishing', 'malware', 'ransomware', 'data_breach', 'vgs', 'other') NOT NULL DEFAULT 'other', \`stage\` enum ('gathered', 'reviewed', 'published') NOT NULL DEFAULT 'gathered', \`type\` enum ('manual', 'automatic') NOT NULL DEFAULT 'automatic', \`is_verified\` tinyint NOT NULL DEFAULT 0, \`verified_by\` varchar(255) NULL, \`verified_at\` timestamp NULL, \`score\` int NULL DEFAULT '0', \`severity\` int NULL, \`is_relevant_to_nepal\` tinyint NOT NULL DEFAULT 0, \`sent_to_newsletter\` tinyint NOT NULL DEFAULT 0, \`newsletter_id\` varchar(255) 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), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`appointments\` (\`id\` int NOT NULL AUTO_INCREMENT, \`full_name\` varchar(255) NOT NULL, \`title\` varchar(255) NOT NULL, \`notes\` varchar(255) NULL, \`appointment_date\` date NOT NULL, \`appointment_time\` time NOT NULL, \`duration\` int NOT NULL, \`status\` enum ('pending', 'approved', 'rejected', 'completed') NOT NULL DEFAULT 'pending', \`user_id\` int NOT NULL, \`phone\` int 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), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`city_block_ipv4\` (\`id\` int NOT NULL AUTO_INCREMENT, \`network\` varchar(255) NOT NULL, \`geoname_id\` int NULL, \`registered_country_geoname_id\` int NULL, \`represented_country_geoname_id\` int NULL, \`is_anonymous_proxy\` tinyint NULL DEFAULT 0, \`is_satellite_provider\` tinyint NULL DEFAULT 0, \`postal_code\` varchar(255) NULL, \`latitude\` float NULL, \`longitude\` float NULL, \`accuracy_radius\` float NULL, \`is_anycast\` tinyint NULL DEFAULT 0, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`city_block_ipv6\` (\`id\` int NOT NULL AUTO_INCREMENT, \`network\` varchar(50) NOT NULL, \`geoname_id\` int NULL, \`registered_country_geoname_id\` int NULL, \`represented_country_geoname_id\` int NULL, \`latitude\` float NULL, \`longitude\` float NULL, \`accuracy_radius\` float NULL, \`postal_code\` varchar(50) NULL, \`is_anonymous_proxy\` tinyint NULL, \`is_satellite_provider\` tinyint NULL, \`is_anycast\` tinyint NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`ips\` (\`id\` int NOT NULL AUTO_INCREMENT, \`ip\` varchar(255) NOT NULL, \`city\` varchar(255) NULL, \`country\` varchar(255) NULL, \`user_agent\` varchar(255) NULL, \`asn\` 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), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`geo_names\` (\`id\` int NOT NULL AUTO_INCREMENT, \`geoname_id\` int NOT NULL, \`locale_code\` varchar(255) NULL, \`continent_code\` varchar(255) NULL, \`continent_name\` varchar(255) NULL, \`country_iso_code\` varchar(255) NULL, \`country_name\` varchar(255) NULL DEFAULT 'N/A', \`subdivision_1_iso_code\` varchar(255) NULL, \`subdivision_1_name\` varchar(255) NULL, \`city_name\` varchar(255) NULL, \`time_zone\` varchar(255) NULL, UNIQUE INDEX \`IDX_2cb6561848ac13dea90dbebc34\` (\`geoname_id\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`asn_data\` (\`id\` int NOT NULL AUTO_INCREMENT, \`start_ip\` varchar(255) NOT NULL, \`end_ip\` varchar(255) NOT NULL, \`asn\` varchar(255) NOT NULL, \`name\` varchar(255) NOT NULL, \`domain\` varchar(255) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`role_permissions\` (\`role_id\` int NOT NULL, \`permission_id\` int NOT NULL, INDEX \`IDX_178199805b901ccd220ab7740e\` (\`role_id\`), INDEX \`IDX_17022daf3f885f7d35423e9971\` (\`permission_id\`), PRIMARY KEY (\`role_id\`, \`permission_id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`ALTER TABLE \`comments\` ADD CONSTRAINT \`FK_e6d38899c31997c45d128a8973b\` FOREIGN KEY (\`author_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`comments\` ADD CONSTRAINT \`FK_be8180d9b44a05e449b85f5b773\` FOREIGN KEY (\`ticket_id\`) REFERENCES \`tickets\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`tickets\` ADD CONSTRAINT \`FK_67714d39d16d1ea5a234b05b094\` FOREIGN KEY (\`author_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`tickets\` ADD CONSTRAINT \`FK_dff6e2b44c9b5e177114588772f\` FOREIGN KEY (\`assignee_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`api_keys\` ADD CONSTRAINT \`FK_a3baee01d8408cd3c0f89a9a973\` FOREIGN KEY (\`user_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`question_responses\` ADD CONSTRAINT \`FK_cc06a7e625d02cb5f9ebc267bfc\` FOREIGN KEY (\`report_id\`) REFERENCES \`reports\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`blog_comments\` ADD CONSTRAINT \`FK_044ee27753221b2b8860f8926c7\` FOREIGN KEY (\`blog_id\`) REFERENCES \`blogs\`(\`blog_id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`blog_translations\` ADD CONSTRAINT \`FK_802900bace2abc0226bee2833b2\` FOREIGN KEY (\`blog_id\`) REFERENCES \`blogs\`(\`blog_id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`role_permissions\` ADD CONSTRAINT \`FK_178199805b901ccd220ab7740ec\` FOREIGN KEY (\`role_id\`) REFERENCES \`roles\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`);
        await queryRunner.query(`ALTER TABLE \`role_permissions\` ADD CONSTRAINT \`FK_17022daf3f885f7d35423e9971e\` FOREIGN KEY (\`permission_id\`) REFERENCES \`permissions\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`role_permissions\` DROP FOREIGN KEY \`FK_17022daf3f885f7d35423e9971e\``);
        await queryRunner.query(`ALTER TABLE \`role_permissions\` DROP FOREIGN KEY \`FK_178199805b901ccd220ab7740ec\``);
        await queryRunner.query(`ALTER TABLE \`blog_translations\` DROP FOREIGN KEY \`FK_802900bace2abc0226bee2833b2\``);
        await queryRunner.query(`ALTER TABLE \`blog_comments\` DROP FOREIGN KEY \`FK_044ee27753221b2b8860f8926c7\``);
        await queryRunner.query(`ALTER TABLE \`question_responses\` DROP FOREIGN KEY \`FK_cc06a7e625d02cb5f9ebc267bfc\``);
        await queryRunner.query(`ALTER TABLE \`api_keys\` DROP FOREIGN KEY \`FK_a3baee01d8408cd3c0f89a9a973\``);
        await queryRunner.query(`ALTER TABLE \`tickets\` DROP FOREIGN KEY \`FK_dff6e2b44c9b5e177114588772f\``);
        await queryRunner.query(`ALTER TABLE \`tickets\` DROP FOREIGN KEY \`FK_67714d39d16d1ea5a234b05b094\``);
        await queryRunner.query(`ALTER TABLE \`comments\` DROP FOREIGN KEY \`FK_be8180d9b44a05e449b85f5b773\``);
        await queryRunner.query(`ALTER TABLE \`comments\` DROP FOREIGN KEY \`FK_e6d38899c31997c45d128a8973b\``);
        await queryRunner.query(`DROP INDEX \`IDX_17022daf3f885f7d35423e9971\` ON \`role_permissions\``);
        await queryRunner.query(`DROP INDEX \`IDX_178199805b901ccd220ab7740e\` ON \`role_permissions\``);
        await queryRunner.query(`DROP TABLE \`role_permissions\``);
        await queryRunner.query(`DROP TABLE \`asn_data\``);
        await queryRunner.query(`DROP INDEX \`IDX_2cb6561848ac13dea90dbebc34\` ON \`geo_names\``);
        await queryRunner.query(`DROP TABLE \`geo_names\``);
        await queryRunner.query(`DROP TABLE \`ips\``);
        await queryRunner.query(`DROP TABLE \`city_block_ipv6\``);
        await queryRunner.query(`DROP TABLE \`city_block_ipv4\``);
        await queryRunner.query(`DROP TABLE \`appointments\``);
        await queryRunner.query(`DROP TABLE \`alerts\``);
        await queryRunner.query(`DROP TABLE \`blog_translations\``);
        await queryRunner.query(`DROP TABLE \`blogs\``);
        await queryRunner.query(`DROP TABLE \`blog_comments\``);
        await queryRunner.query(`DROP TABLE \`newsletter_logs\``);
        await queryRunner.query(`DROP INDEX \`IDX_1a7163c08f0e57bd1c9821508b\` ON \`subscribers\``);
        await queryRunner.query(`DROP TABLE \`subscribers\``);
        await queryRunner.query(`DROP TABLE \`reports\``);
        await queryRunner.query(`DROP TABLE \`question_responses\``);
        await queryRunner.query(`DROP INDEX \`IDX_48ce552495d14eae9b187bb671\` ON \`permissions\``);
        await queryRunner.query(`DROP TABLE \`permissions\``);
        await queryRunner.query(`DROP INDEX \`IDX_648e3f5447f725579d7d4ffdfb\` ON \`roles\``);
        await queryRunner.query(`DROP TABLE \`roles\``);
        await queryRunner.query(`DROP INDEX \`IDX_97672ac88f789774dd47f7c8be\` ON \`users\``);
        await queryRunner.query(`DROP TABLE \`users\``);
        await queryRunner.query(`DROP INDEX \`IDX_e42cf55faeafdcce01a82d2484\` ON \`api_keys\``);
        await queryRunner.query(`DROP TABLE \`api_keys\``);
        await queryRunner.query(`DROP TABLE \`tickets\``);
        await queryRunner.query(`DROP TABLE \`comments\``);
    }

}
