import { MigrationInterface, QueryRunner } from "typeorm";

export class ModifyBreachCheckAlert1748773850077 implements MigrationInterface {
    name = 'ModifyBreachCheckAlert1748773850077'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE \`breach_check_organizations\` (\`id\` int NOT NULL AUTO_INCREMENT, \`org_name\` varchar(255) NOT NULL, \`subscription_tier\` enum ('FREE', 'BASIC', 'PROFESSIONAL', 'ENTERPRISE') NOT NULL DEFAULT 'FREE', \`contact_email\` varchar(255) NOT NULL, \`is_active\` tinyint NOT NULL DEFAULT 1, \`user_id\` 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), UNIQUE INDEX \`IDX_f3acb3838e92a6eb7aaf8642fc\` (\`org_name\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`breach_check_results\` (\`id\` int NOT NULL AUTO_INCREMENT, \`website_id\` int NOT NULL, \`check_type\` enum ('AVAILABILITY', 'RESPONSE_TIME', 'DEFACEMENT', 'SSL_CERTIFICATE', 'DNS_CHANGES', 'CONTENT_CHANGE', 'MALWARE_SCAN', 'PHISHING_CHECK', 'BROKEN_LINKS', 'SEO_METRICS', 'ACCESSIBILITY', 'PERFORMANCE') NOT NULL, \`timestamp\` timestamp NOT NULL, \`status\` enum ('PASS', 'WARNING', 'FAIL', 'ERROR') NOT NULL DEFAULT 'PASS', \`severity\` enum ('INFO', 'LOW', 'MEDIUM', 'HIGH', 'CRITICAL') NOT NULL DEFAULT 'INFO', \`details\` json NULL, \`rawData\` text NULL, \`alert_generated\` tinyint NOT NULL DEFAULT 0, \`response_time_ms\` int NULL, INDEX \`IDX_c51171f61958ace5ea0ee06cc2\` (\`check_type\`), INDEX \`IDX_503779951edba8438f7d3b9326\` (\`timestamp\`), INDEX \`IDX_17391c79c92b1ac7742c65f6ec\` (\`status\`), INDEX \`IDX_d2d6f606673a2c43d17cd95325\` (\`severity\`), INDEX \`IDX_98b30a566216874321d00a8712\` (\`website_id\`, \`check_type\`, \`timestamp\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`breach_check_websites\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(255) NOT NULL, \`url\` varchar(255) NOT NULL, \`category\` enum ('GOVERNMENT_FEDERAL', 'GOVERNMENT_PROVINCIAL', 'GOVERNMENT_LOCAL', 'SECURITY_AGENCY', 'LAW_ENFORCEMENT', 'FINANCIAL_INSTITUTION', 'INSURANCE', 'BANKING', 'PUBLIC_SERVICE', 'UTILITIES', 'TRANSPORTATION', 'EDUCATION_UNIVERSITY', 'EDUCATION_SCHOOL', 'EDUCATION_RESOURCE', 'HEALTHCARE_HOSPITAL', 'HEALTHCARE_CLINIC', 'HEALTHCARE_RESEARCH', 'BUSINESS_ENTERPRISE', 'BUSINESS_SME', 'ECOMMERCE', 'INFRASTRUCTURE', 'MEDIA', 'NON_PROFIT', 'TECHNOLOGY', 'OTHER') NOT NULL DEFAULT 'OTHER', \`monitoring_frequency_minutes\` int NOT NULL DEFAULT '60', \`stage\` enum ('Scanned', 'Review', 'Archived', 'Deleted') NOT NULL, \`is_false_positive\` tinyint NOT NULL DEFAULT 0, \`last_monitored_at\` timestamp NULL, \`operationalStatus\` enum ('ONLINE', 'DEGRADED', 'OFFLINE', 'COMPROMISED', 'MAINTENANCE', 'UNKNOWN') NOT NULL DEFAULT 'UNKNOWN', \`enabledChecks\` text NOT NULL, \`receive_alerts\` tinyint NOT NULL DEFAULT 1, \`alert_threshold\` int NOT NULL DEFAULT '50', \`notes\` varchar(255) NULL, \`tags\` text NOT NULL, \`organization_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), INDEX \`IDX_d8f0335cfea877698aaed3f950\` (\`name\`), UNIQUE INDEX \`IDX_d02bda6e6146caf785f9ec2cd4\` (\`url\`), INDEX \`IDX_3937a47d8890be5de6668733dc\` (\`category\`), INDEX \`IDX_600e137555c96b2dfd3fb572fe\` (\`stage\`), INDEX \`IDX_9a9aa8e74b65c4b2392aac1cf1\` (\`operationalStatus\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`breach_check_website_baselines\` (\`id\` int NOT NULL AUTO_INCREMENT, \`website_id\` int NOT NULL, \`main_content_hash\` varchar(64) NULL COMMENT 'Primary MD5 hash of main content (title|headings|meta|body) for instant comparison', \`contentSnapshot\` text NULL COMMENT 'JSON snapshot of content signature for detailed analysis', \`title_hash\` varchar(64) NULL COMMENT 'Hash of page title', \`structure_hash\` varchar(64) NULL COMMENT 'Hash of page structure (DOM elements count)', \`body_text_hash\` varchar(64) NULL COMMENT 'Hash of main body text content', \`meta_desc_hash\` varchar(64) NULL COMMENT 'Hash of meta description', \`last_content_update\` timestamp NULL, \`http_status_baseline\` int NULL COMMENT 'Expected HTTP status code', \`discovered_routes\` json NULL COMMENT 'Array of discovered routes with their content hashes', \`routes_hash\` varchar(64) NULL COMMENT 'Hash of all routes structure for detecting route changes', \`last_crawl_update\` timestamp NULL, \`ai_analysis_enabled\` tinyint NOT NULL COMMENT 'Whether to use AI analysis for content changes' DEFAULT 1, \`crawling_enabled\` tinyint NOT NULL COMMENT 'Whether to enable route crawling and monitoring' DEFAULT 1, \`max_crawl_depth\` int NOT NULL COMMENT 'Maximum depth for route crawling' DEFAULT '2', \`max_crawl_urls\` int NOT NULL COMMENT 'Maximum number of URLs to crawl' DEFAULT '20', \`last_updated\` timestamp NULL COMMENT 'Timestamp for tracking baseline refresh periods', \`baseline_version\` int NOT NULL COMMENT 'Version number of the baseline, incremented with significant updates' DEFAULT '1', \`sensitivity_level\` int NOT NULL COMMENT 'Sensitivity level for defacement detection (0-100)' DEFAULT '50', \`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_d5058ca0f327d054ba9e422194\` (\`website_id\`), INDEX \`IDX_cf483af159b399aa2b2683161d\` (\`main_content_hash\`), INDEX \`IDX_33281ee56d484fdca1bcf3f65c\` (\`routes_hash\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`ALTER TABLE \`breach_check_results\` ADD CONSTRAINT \`FK_4dcfec06f8b28c91d77bf15f652\` FOREIGN KEY (\`website_id\`) REFERENCES \`breach_check_websites\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`breach_check_websites\` ADD CONSTRAINT \`FK_6f7009e1ab7f18e50fcfc41f67a\` FOREIGN KEY (\`organization_id\`) REFERENCES \`breach_check_organizations\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`breach_check_website_baselines\` ADD CONSTRAINT \`FK_d5058ca0f327d054ba9e4221944\` FOREIGN KEY (\`website_id\`) REFERENCES \`breach_check_websites\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`breach_check_website_baselines\` DROP FOREIGN KEY \`FK_d5058ca0f327d054ba9e4221944\``);
        await queryRunner.query(`ALTER TABLE \`breach_check_websites\` DROP FOREIGN KEY \`FK_6f7009e1ab7f18e50fcfc41f67a\``);
        await queryRunner.query(`ALTER TABLE \`breach_check_results\` DROP FOREIGN KEY \`FK_4dcfec06f8b28c91d77bf15f652\``);
        await queryRunner.query(`DROP INDEX \`IDX_33281ee56d484fdca1bcf3f65c\` ON \`breach_check_website_baselines\``);
        await queryRunner.query(`DROP INDEX \`IDX_cf483af159b399aa2b2683161d\` ON \`breach_check_website_baselines\``);
        await queryRunner.query(`DROP INDEX \`IDX_d5058ca0f327d054ba9e422194\` ON \`breach_check_website_baselines\``);
        await queryRunner.query(`DROP TABLE \`breach_check_website_baselines\``);
        await queryRunner.query(`DROP INDEX \`IDX_9a9aa8e74b65c4b2392aac1cf1\` ON \`breach_check_websites\``);
        await queryRunner.query(`DROP INDEX \`IDX_600e137555c96b2dfd3fb572fe\` ON \`breach_check_websites\``);
        await queryRunner.query(`DROP INDEX \`IDX_3937a47d8890be5de6668733dc\` ON \`breach_check_websites\``);
        await queryRunner.query(`DROP INDEX \`IDX_d02bda6e6146caf785f9ec2cd4\` ON \`breach_check_websites\``);
        await queryRunner.query(`DROP INDEX \`IDX_d8f0335cfea877698aaed3f950\` ON \`breach_check_websites\``);
        await queryRunner.query(`DROP TABLE \`breach_check_websites\``);
        await queryRunner.query(`DROP INDEX \`IDX_98b30a566216874321d00a8712\` ON \`breach_check_results\``);
        await queryRunner.query(`DROP INDEX \`IDX_d2d6f606673a2c43d17cd95325\` ON \`breach_check_results\``);
        await queryRunner.query(`DROP INDEX \`IDX_17391c79c92b1ac7742c65f6ec\` ON \`breach_check_results\``);
        await queryRunner.query(`DROP INDEX \`IDX_503779951edba8438f7d3b9326\` ON \`breach_check_results\``);
        await queryRunner.query(`DROP INDEX \`IDX_c51171f61958ace5ea0ee06cc2\` ON \`breach_check_results\``);
        await queryRunner.query(`DROP TABLE \`breach_check_results\``);
        await queryRunner.query(`DROP INDEX \`IDX_f3acb3838e92a6eb7aaf8642fc\` ON \`breach_check_organizations\``);
        await queryRunner.query(`DROP TABLE \`breach_check_organizations\``);
    }

}
