import { MigrationInterface, QueryRunner } from "typeorm";

export class  $npmConfigName1773840393428 implements MigrationInterface {
    name = ' $npmConfigName1773840393428'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE \`agencies\` (\`id\` int UNSIGNED NOT NULL AUTO_INCREMENT, \`name\` varchar(100) NOT NULL, \`country\` varchar(80) NOT NULL DEFAULT 'Madagascar', \`manager_id\` int UNSIGNED NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`teams\` (\`id\` int UNSIGNED NOT NULL AUTO_INCREMENT, \`name\` varchar(100) NOT NULL, \`min_presence\` int NOT NULL DEFAULT '1', \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`agency_id\` int UNSIGNED NULL, \`manager_id\` int UNSIGNED NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`users\` (\`id\` int UNSIGNED NOT NULL AUTO_INCREMENT, \`matricule\` varchar(20) NOT NULL, \`first_name\` varchar(100) NOT NULL, \`last_name\` varchar(100) NOT NULL, \`email\` varchar(150) NOT NULL, \`password_hash\` varchar(255) NOT NULL, \`role\` enum ('employee', 'team_lead', 'agency_manager', 'director', 'admin') NOT NULL, \`work_schedule\` enum ('4d', '5d', '6d') NOT NULL DEFAULT '5d', \`hired_at\` date NOT NULL, \`is_active\` tinyint NOT NULL DEFAULT 1, \`avatar_url\` 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), \`team_id\` int UNSIGNED NULL, \`agency_id\` int UNSIGNED NULL, UNIQUE INDEX \`IDX_97f73e8e81f7b794fd2455b692\` (\`matricule\`), UNIQUE INDEX \`IDX_97672ac88f789774dd47f7c8be\` (\`email\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`leave_types\` (\`id\` int UNSIGNED NOT NULL AUTO_INCREMENT, \`code\` varchar(20) NOT NULL, \`label\` varchar(100) NOT NULL, \`requires_attachment\` tinyint NOT NULL DEFAULT 0, \`deducts_balance\` tinyint NOT NULL DEFAULT 1, \`color_hex\` varchar(7) NOT NULL DEFAULT '#1E88E5', \`is_active\` tinyint NOT NULL DEFAULT 1, UNIQUE INDEX \`IDX_600530eb1d9f853dd746e5819c\` (\`code\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`leave_requests\` (\`id\` int UNSIGNED NOT NULL AUTO_INCREMENT, \`start_date\` date NOT NULL, \`end_date\` date NOT NULL, \`days_count\` decimal(4,1) NOT NULL, \`reason\` text NULL, \`attachment_url\` varchar(255) NULL, \`status\` enum ('pending_n1', 'pending_n2', 'pending_n3', 'approved', 'rejected', 'cancelled') NOT NULL DEFAULT 'pending_n1', \`n1_validated_at\` timestamp NULL, \`n1_comment\` text NULL, \`n2_validated_at\` timestamp NULL, \`n2_comment\` text NULL, \`n3_validated_at\` timestamp NULL, \`n3_comment\` 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), \`user_id\` int UNSIGNED NULL, \`leave_type_id\` int UNSIGNED NULL, \`n1_validator_id\` int UNSIGNED NULL, \`n2_validator_id\` int UNSIGNED NULL, \`n3_validator_id\` int UNSIGNED NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`CREATE TABLE \`leave_balances\` (\`id\` int UNSIGNED NOT NULL AUTO_INCREMENT, \`year\` year NOT NULL, \`acquired\` decimal(5,1) NOT NULL DEFAULT '0.0', \`taken\` decimal(5,1) NOT NULL DEFAULT '0.0', \`adjusted\` decimal(5,1) NOT NULL DEFAULT '0.0', \`adjustment_reason\` text NULL, \`user_id\` int UNSIGNED NULL, \`leave_type_id\` int UNSIGNED NULL, UNIQUE INDEX \`IDX_c96727e7b6f0420af2593ca754\` (\`user_id\`, \`leave_type_id\`, \`year\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
        await queryRunner.query(`ALTER TABLE \`agencies\` ADD CONSTRAINT \`FK_0168b82dc89c1101c66d0e984d8\` FOREIGN KEY (\`manager_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`teams\` ADD CONSTRAINT \`FK_b028103a23f81072861ca4366c8\` FOREIGN KEY (\`agency_id\`) REFERENCES \`agencies\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`teams\` ADD CONSTRAINT \`FK_9a06e17b915db57b2d9192672a5\` FOREIGN KEY (\`manager_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`users\` ADD CONSTRAINT \`FK_1208ee1db5ddb64b48a86b46a61\` FOREIGN KEY (\`team_id\`) REFERENCES \`teams\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`users\` ADD CONSTRAINT \`FK_6fdb3282cea9ba5877ab476aa76\` FOREIGN KEY (\`agency_id\`) REFERENCES \`agencies\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`leave_requests\` ADD CONSTRAINT \`FK_6d320737541c7c4d2a6f0f9d911\` FOREIGN KEY (\`user_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`leave_requests\` ADD CONSTRAINT \`FK_54a57db316598806786c2b95323\` FOREIGN KEY (\`leave_type_id\`) REFERENCES \`leave_types\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`leave_requests\` ADD CONSTRAINT \`FK_5d3ed8cefa7827d0347b968d4aa\` FOREIGN KEY (\`n1_validator_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`leave_requests\` ADD CONSTRAINT \`FK_104b1b3bc98a6f7273f101ab550\` FOREIGN KEY (\`n2_validator_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`leave_requests\` ADD CONSTRAINT \`FK_757f6cf287ee0bf1f94f7c49668\` FOREIGN KEY (\`n3_validator_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`leave_balances\` ADD CONSTRAINT \`FK_89ffc3253557b60d4d979ffe50c\` FOREIGN KEY (\`user_id\`) REFERENCES \`users\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`leave_balances\` ADD CONSTRAINT \`FK_d64da0a991d2f4d23d86031530c\` FOREIGN KEY (\`leave_type_id\`) REFERENCES \`leave_types\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`leave_balances\` DROP FOREIGN KEY \`FK_d64da0a991d2f4d23d86031530c\``);
        await queryRunner.query(`ALTER TABLE \`leave_balances\` DROP FOREIGN KEY \`FK_89ffc3253557b60d4d979ffe50c\``);
        await queryRunner.query(`ALTER TABLE \`leave_requests\` DROP FOREIGN KEY \`FK_757f6cf287ee0bf1f94f7c49668\``);
        await queryRunner.query(`ALTER TABLE \`leave_requests\` DROP FOREIGN KEY \`FK_104b1b3bc98a6f7273f101ab550\``);
        await queryRunner.query(`ALTER TABLE \`leave_requests\` DROP FOREIGN KEY \`FK_5d3ed8cefa7827d0347b968d4aa\``);
        await queryRunner.query(`ALTER TABLE \`leave_requests\` DROP FOREIGN KEY \`FK_54a57db316598806786c2b95323\``);
        await queryRunner.query(`ALTER TABLE \`leave_requests\` DROP FOREIGN KEY \`FK_6d320737541c7c4d2a6f0f9d911\``);
        await queryRunner.query(`ALTER TABLE \`users\` DROP FOREIGN KEY \`FK_6fdb3282cea9ba5877ab476aa76\``);
        await queryRunner.query(`ALTER TABLE \`users\` DROP FOREIGN KEY \`FK_1208ee1db5ddb64b48a86b46a61\``);
        await queryRunner.query(`ALTER TABLE \`teams\` DROP FOREIGN KEY \`FK_9a06e17b915db57b2d9192672a5\``);
        await queryRunner.query(`ALTER TABLE \`teams\` DROP FOREIGN KEY \`FK_b028103a23f81072861ca4366c8\``);
        await queryRunner.query(`ALTER TABLE \`agencies\` DROP FOREIGN KEY \`FK_0168b82dc89c1101c66d0e984d8\``);
        await queryRunner.query(`DROP INDEX \`IDX_c96727e7b6f0420af2593ca754\` ON \`leave_balances\``);
        await queryRunner.query(`DROP TABLE \`leave_balances\``);
        await queryRunner.query(`DROP TABLE \`leave_requests\``);
        await queryRunner.query(`DROP INDEX \`IDX_600530eb1d9f853dd746e5819c\` ON \`leave_types\``);
        await queryRunner.query(`DROP TABLE \`leave_types\``);
        await queryRunner.query(`DROP INDEX \`IDX_97672ac88f789774dd47f7c8be\` ON \`users\``);
        await queryRunner.query(`DROP INDEX \`IDX_97f73e8e81f7b794fd2455b692\` ON \`users\``);
        await queryRunner.query(`DROP TABLE \`users\``);
        await queryRunner.query(`DROP TABLE \`teams\``);
        await queryRunner.query(`DROP TABLE \`agencies\``);
    }

}
