-- Migration: Ajouter le support des conventions spécifiques à convention_achevement
-- Date: 2025-01-XX
-- Description: Ajoute convention_specifique_id et entity_type pour supporter les conventions spécifiques

-- Étape 1: Supprimer temporairement la contrainte de clé étrangère existante (si elle existe)
SET @constraint_name = NULL;
SELECT CONSTRAINT_NAME INTO @constraint_name
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'convention_achevement'
  AND COLUMN_NAME = 'convention_id'
  AND REFERENCED_TABLE_NAME = 'conventions'
LIMIT 1;

SET @drop_constraint = IF(@constraint_name IS NOT NULL,
    CONCAT('ALTER TABLE `convention_achevement` DROP FOREIGN KEY `', @constraint_name, '`;'),
    'SELECT "Contrainte non trouvée ou déjà supprimée." AS message;');

PREPARE stmt FROM @drop_constraint;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Étape 2: Supprimer l'index unique sur convention_id (il sera recréé plus tard)
SET @index_name = NULL;
SELECT INDEX_NAME INTO @index_name
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'convention_achevement'
  AND INDEX_NAME = 'idx_convention_unique'
LIMIT 1;

SET @drop_index = IF(@index_name IS NOT NULL,
    CONCAT('ALTER TABLE `convention_achevement` DROP INDEX `', @index_name, '`;'),
    'SELECT "Index non trouvé ou déjà supprimé." AS message;');

PREPARE stmt FROM @drop_index;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Étape 3: Rendre convention_id nullable (car il peut être null si convention_specifique_id est utilisé)
ALTER TABLE `convention_achevement` 
MODIFY COLUMN `convention_id` INT NULL DEFAULT NULL COMMENT "ID de la convention (peut être null si convention_specifique_id est utilisé)";

-- Étape 4: Ajouter les colonnes convention_specifique_id et entity_type
ALTER TABLE `convention_achevement` 
ADD COLUMN `convention_specifique_id` INT NULL DEFAULT NULL COMMENT "ID de la convention spécifique" AFTER `convention_id`,
ADD COLUMN `entity_type` ENUM('Convention', 'Convention_Specifique') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT 'Convention' COMMENT "Type d'entité" AFTER `convention_specifique_id`;

-- Étape 5: Recréer la contrainte de clé étrangère pour convention_id (maintenant nullable)
ALTER TABLE `convention_achevement`
ADD CONSTRAINT `convention_achevement_ibfk_1` FOREIGN KEY (`convention_id`) REFERENCES `conventions` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

-- Étape 6: Ajouter les index
ALTER TABLE `convention_achevement`
ADD INDEX `idx_convention_specifique` (`convention_specifique_id` ASC) USING BTREE,
ADD INDEX `idx_entity_type` (`entity_type` ASC) USING BTREE;

-- Étape 7: Ajouter la contrainte de clé étrangère pour convention_specifique_id
ALTER TABLE `convention_achevement`
ADD CONSTRAINT `convention_achevement_ibfk_3` FOREIGN KEY (`convention_specifique_id`) REFERENCES `convention_specifique` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

-- Étape 8: Recréer l'index unique pour convention_id (pour les conventions normales)
ALTER TABLE `convention_achevement`
ADD UNIQUE INDEX `idx_convention_unique` (`convention_id` ASC) USING BTREE;

-- Étape 9: Ajouter un index unique pour convention_specifique_id (pour les conventions spécifiques)
ALTER TABLE `convention_achevement`
ADD UNIQUE INDEX `idx_convention_specifique_unique` (`convention_specifique_id` ASC) USING BTREE;

-- Étape 10: Mettre à jour les données existantes pour avoir entity_type = 'Convention'
UPDATE `convention_achevement` SET `entity_type` = 'Convention' WHERE `entity_type` IS NULL AND `convention_id` IS NOT NULL;

-- Message de confirmation
SELECT "Migration 044: Le support des conventions spécifiques a été ajouté à convention_achevement." AS message;

