import { MigrationInterface, QueryRunner } from "typeorm";

export class AddAttackcheckEntity1747411348007 implements MigrationInterface {
    name = 'AddAttackcheckEntity1747411348007'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE \`monitoring_incidents\` (\`id\` int NOT NULL AUTO_INCREMENT, \`website_id\` int NOT NULL, \`status\` enum ('OPEN', 'INVESTIGATING', 'ACKNOWLEDGED', 'MITIGATING', 'RESOLVED', 'CLOSED') NOT NULL DEFAULT 'OPEN', \`severity\` enum ('INFO', 'LOW', 'MEDIUM', 'HIGH', 'CRITICAL') NOT NULL DEFAULT 'MEDIUM', \`issueTypes\` text NOT NULL, \`detected_at\` timestamp NOT NULL, \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`resolved_at\` timestamp NULL, \`title\` varchar(255) NOT NULL, \`description\` text NOT NULL, \`related_check_result_ids\` text NOT NULL, \`assigned_to\` int NULL, \`resolution_notes\` text NULL, INDEX \`IDX_32d76701125d41b91326f230c5\` (\`status\`), INDEX \`IDX_ba58bcca306a399aaffca45217\` (\`severity\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`monitoring_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 NOT NULL, \`rawData\` text NULL, \`alert_generated\` tinyint NOT NULL DEFAULT 0, \`response_time_ms\` int NULL, \`related_incident_id\` int NULL, INDEX \`IDX_9f3842ba7e40e40d34e2ac6038\` (\`check_type\`), INDEX \`IDX_320a1e9fdd05493e8bd9ddde8b\` (\`timestamp\`), INDEX \`IDX_15fcabd2f4efbe5e975c9fa817\` (\`status\`), INDEX \`IDX_37721ad1f3bbd1e39e28d82060\` (\`severity\`), INDEX \`IDX_a156e680f3d8b63b646b769f46\` (\`website_id\`, \`check_type\`, \`timestamp\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`monitored_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', \`priority\` int(1) NOT NULL COMMENT 'Priority level (1=highest, 5=lowest)', \`monitoring_frequency_minutes\` int NOT NULL DEFAULT '60', \`last_monitored_at\` timestamp NULL, \`operationalStatus\` enum ('ONLINE', 'DEGRADED', 'OFFLINE', 'COMPROMISED', 'MAINTENANCE', 'UNKNOWN') NOT NULL DEFAULT 'UNKNOWN', \`enabledChecks\` text NOT NULL, \`notes\` varchar(255) NULL, \`tags\` text NOT NULL, \`organization_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), INDEX \`IDX_d7386e3117d786c546b72fc235\` (\`name\`), UNIQUE INDEX \`IDX_002537e8f02cc6ad920ee118f4\` (\`url\`), INDEX \`IDX_ee0fdfe891540f0e242ba6f35d\` (\`category\`), INDEX \`IDX_83f65d99255ac52254ee9df738\` (\`operationalStatus\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`contact_info\` (\`id\` int NOT NULL AUTO_INCREMENT, \`website_id\` int NOT NULL, \`type\` enum ('EMAIL', 'PHONE', 'SMS', 'WEBHOOK', 'OTHER') NOT NULL DEFAULT 'EMAIL', \`value\` varchar(255) NOT NULL, \`name\` varchar(100) NULL, \`receive_alerts\` tinyint NOT NULL DEFAULT 1, \`alert_threshold\` enum ('INFO', 'LOW', 'MEDIUM', 'HIGH', 'CRITICAL') NOT NULL DEFAULT 'MEDIUM', \`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 \`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, \`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_5f4ddfcf143770483d20616170\` (\`org_name\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`website_baselines\` (\`id\` int NOT NULL AUTO_INCREMENT, \`website_id\` int NOT NULL, \`contentBaseline\` text NULL, \`content_baseline_hash\` varchar(64) NULL, \`title_hash\` varchar(64) NULL, \`structure_hash\` varchar(64) NULL, \`last_content_update\` timestamp NULL, \`last_updated\` timestamp NULL COMMENT 'Timestamp for tracking baseline refresh periods', \`defacement_sensitivity\` int NOT NULL COMMENT 'Sensitivity level for defacement detection (0-100)' DEFAULT '50', \`custom_defacement_keywords\` json NULL COMMENT 'Custom keywords to check for defacement detection', \`excluded_elements\` json NULL COMMENT 'CSS selectors for elements to exclude from content comparison', \`dnsBaseline\` json NULL, \`last_dns_update\` timestamp NULL, \`sslBaseline\` json NULL, \`last_ssl_update\` timestamp NULL, \`http_headers_baseline\` json NULL COMMENT 'Baseline for HTTP headers monitoring', \`http_status_baseline\` int NULL COMMENT 'Expected HTTP status code', \`last_http_update\` 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), \`baseline_version\` int NOT NULL COMMENT 'Version number of the baseline, incremented with significant updates' DEFAULT '1', UNIQUE INDEX \`IDX_c19c358e58fd27a638e841ea66\` (\`website_id\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`ALTER TABLE \`alerts\` CHANGE \`attack_type\` \`attack_type\` enum ('ddos', 'phishing', 'malware', 'ransomware', 'data_breach', 'vgs', 'exploit', 'scam', 'social_engineering', 'spam', 'botnet', 'apt', 'sql_injection', 'xss', 'csrf', 'mitm', 'dns_attack', 'physical_attack', 'network_attack', 'application_attack', 'cloud_attack', 'iot_attack', 'supply_chain_attack', 'insider_threat', 'zero_day', 'privilege_escalation', 'denial_of_service', 'account_compromise', 'credential_stuffing', 'domain_hijacking', 'email_compromise', 'web_application_attack', 'cloud_service_attack', 'social_media_attack', 'mobile_attack', 'api_attack', 'cyber_attack', 'other') NOT NULL DEFAULT 'other'`);
        await queryRunner.query(`ALTER TABLE \`monitoring_incidents\` ADD CONSTRAINT \`FK_48740d7d2c0e8347145d5952edc\` FOREIGN KEY (\`website_id\`) REFERENCES \`monitored_websites\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`monitoring_check_results\` ADD CONSTRAINT \`FK_b74393c95cadca12e83c42cf59e\` FOREIGN KEY (\`website_id\`) REFERENCES \`monitored_websites\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`monitoring_check_results\` ADD CONSTRAINT \`FK_a8336199805c69cd599225ce222\` FOREIGN KEY (\`related_incident_id\`) REFERENCES \`monitoring_incidents\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`monitored_websites\` ADD CONSTRAINT \`FK_67785ce0b90775c30ff7b9a5a52\` FOREIGN KEY (\`organization_id\`) REFERENCES \`organizations\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`contact_info\` ADD CONSTRAINT \`FK_1491b8f185f266e59546b56a694\` FOREIGN KEY (\`website_id\`) REFERENCES \`monitored_websites\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`website_baselines\` ADD CONSTRAINT \`FK_c19c358e58fd27a638e841ea662\` FOREIGN KEY (\`website_id\`) REFERENCES \`monitored_websites\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`website_baselines\` DROP FOREIGN KEY \`FK_c19c358e58fd27a638e841ea662\``);
        await queryRunner.query(`ALTER TABLE \`contact_info\` DROP FOREIGN KEY \`FK_1491b8f185f266e59546b56a694\``);
        await queryRunner.query(`ALTER TABLE \`monitored_websites\` DROP FOREIGN KEY \`FK_67785ce0b90775c30ff7b9a5a52\``);
        await queryRunner.query(`ALTER TABLE \`monitoring_check_results\` DROP FOREIGN KEY \`FK_a8336199805c69cd599225ce222\``);
        await queryRunner.query(`ALTER TABLE \`monitoring_check_results\` DROP FOREIGN KEY \`FK_b74393c95cadca12e83c42cf59e\``);
        await queryRunner.query(`ALTER TABLE \`monitoring_incidents\` DROP FOREIGN KEY \`FK_48740d7d2c0e8347145d5952edc\``);
        await queryRunner.query(`ALTER TABLE \`alerts\` CHANGE \`attack_type\` \`attack_type\` enum ('ddos', 'phishing', 'malware', 'ransomware', 'data_breach', 'vgs', 'other') NOT NULL DEFAULT 'other'`);
        await queryRunner.query(`DROP INDEX \`IDX_c19c358e58fd27a638e841ea66\` ON \`website_baselines\``);
        await queryRunner.query(`DROP TABLE \`website_baselines\``);
        await queryRunner.query(`DROP INDEX \`IDX_5f4ddfcf143770483d20616170\` ON \`organizations\``);
        await queryRunner.query(`DROP TABLE \`organizations\``);
        await queryRunner.query(`DROP TABLE \`contact_info\``);
        await queryRunner.query(`DROP INDEX \`IDX_83f65d99255ac52254ee9df738\` ON \`monitored_websites\``);
        await queryRunner.query(`DROP INDEX \`IDX_ee0fdfe891540f0e242ba6f35d\` ON \`monitored_websites\``);
        await queryRunner.query(`DROP INDEX \`IDX_002537e8f02cc6ad920ee118f4\` ON \`monitored_websites\``);
        await queryRunner.query(`DROP INDEX \`IDX_d7386e3117d786c546b72fc235\` ON \`monitored_websites\``);
        await queryRunner.query(`DROP TABLE \`monitored_websites\``);
        await queryRunner.query(`DROP INDEX \`IDX_a156e680f3d8b63b646b769f46\` ON \`monitoring_check_results\``);
        await queryRunner.query(`DROP INDEX \`IDX_37721ad1f3bbd1e39e28d82060\` ON \`monitoring_check_results\``);
        await queryRunner.query(`DROP INDEX \`IDX_15fcabd2f4efbe5e975c9fa817\` ON \`monitoring_check_results\``);
        await queryRunner.query(`DROP INDEX \`IDX_320a1e9fdd05493e8bd9ddde8b\` ON \`monitoring_check_results\``);
        await queryRunner.query(`DROP INDEX \`IDX_9f3842ba7e40e40d34e2ac6038\` ON \`monitoring_check_results\``);
        await queryRunner.query(`DROP TABLE \`monitoring_check_results\``);
        await queryRunner.query(`DROP INDEX \`IDX_ba58bcca306a399aaffca45217\` ON \`monitoring_incidents\``);
        await queryRunner.query(`DROP INDEX \`IDX_32d76701125d41b91326f230c5\` ON \`monitoring_incidents\``);
        await queryRunner.query(`DROP TABLE \`monitoring_incidents\``);
    }

}
