-- CreateTable
CREATE TABLE `Violation` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `displayName` VARCHAR(191) NOT NULL,
    `description` TEXT NULL,
    `severity` ENUM('LOW', 'MEDIUM', 'HIGH', 'CRITICAL') NOT NULL DEFAULT 'MEDIUM',
    `isDefault` BOOLEAN NOT NULL DEFAULT false,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `createdBy` INTEGER NULL,
    `createdByName` VARCHAR(191) NULL,
    `createdByRole` VARCHAR(191) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    UNIQUE INDEX `Violation_name_key`(`name`),
    INDEX `idx_violation_name`(`name`),
    INDEX `idx_violation_default`(`isDefault`),
    INDEX `idx_violation_active`(`isActive`),
    INDEX `idx_violation_severity`(`severity`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `ReportViolation` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `reportId` INTEGER NOT NULL,
    `violationId` INTEGER NOT NULL,
    `notes` TEXT NULL,

    INDEX `idx_report_violation_report`(`reportId`),
    INDEX `idx_report_violation_violation`(`violationId`),
    UNIQUE INDEX `ReportViolation_reportId_violationId_key`(`reportId`, `violationId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `Violation` ADD CONSTRAINT `Violation_createdBy_fkey` FOREIGN KEY (`createdBy`) REFERENCES `user`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `ReportViolation` ADD CONSTRAINT `ReportViolation_reportId_fkey` FOREIGN KEY (`reportId`) REFERENCES `report`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `ReportViolation` ADD CONSTRAINT `ReportViolation_violationId_fkey` FOREIGN KEY (`violationId`) REFERENCES `Violation`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
