-- Migration: Ajouter programmation_id à convention_documents
-- Date: 2025-01-XX
-- Description: Ajoute programmation_id et met à jour entity_type pour supporter Programmation

SET @dbname = DATABASE();
SET @tablename = 'convention_documents';

-- Étape 1: Ajouter la colonne programmation_id
SET @preparedStatement1 = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = 'programmation_id')
  ) > 0,
  'SELECT "La colonne programmation_id existe déjà." AS message;',
  'ALTER TABLE `convention_documents` 
   ADD COLUMN `programmation_id` INT NULL DEFAULT NULL COMMENT "ID de la programmation" AFTER `convention_id`;'
));
PREPARE alterIfNotExists1 FROM @preparedStatement1;
EXECUTE alterIfNotExists1;
DEALLOCATE PREPARE alterIfNotExists1;

-- Étape 2: Mettre à jour l'ENUM entity_type pour inclure Programmation
SET @preparedStatement2 = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = 'entity_type')
  ) > 0,
  'ALTER TABLE `convention_documents` 
   MODIFY COLUMN `entity_type` ENUM(\'Convention_Cadre\', \'Convention_Specifique\', \'Convention\', \'Programmation\', \'Avenant\') 
   CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL;',
  'SELECT "La colonne entity_type n\'existe pas." AS message;'
));
PREPARE alterIfNotExists2 FROM @preparedStatement2;
EXECUTE alterIfNotExists2;
DEALLOCATE PREPARE alterIfNotExists2;

-- Étape 3: Ajouter la contrainte de clé étrangère pour programmation_id
SET @preparedStatement3 = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = 'programmation_id')
      AND (REFERENCED_TABLE_NAME = 'programmations')
  ) > 0,
  'SELECT "La contrainte de clé étrangère pour programmation_id existe déjà." AS message;',
  'ALTER TABLE `convention_documents`
   ADD CONSTRAINT `convention_documents_ibfk_6` FOREIGN KEY (`programmation_id`) REFERENCES `programmations` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;'
));
PREPARE alterIfNotExists3 FROM @preparedStatement3;
EXECUTE alterIfNotExists3;
DEALLOCATE PREPARE alterIfNotExists3;

-- Étape 4: Ajouter l'index pour programmation_id
SET @preparedStatement4 = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (INDEX_NAME = 'programmation_id')
  ) > 0,
  'SELECT "L\'index programmation_id existe déjà." AS message;',
  'ALTER TABLE `convention_documents`
   ADD INDEX `programmation_id` (`programmation_id` ASC) USING BTREE;'
));
PREPARE alterIfNotExists4 FROM @preparedStatement4;
EXECUTE alterIfNotExists4;
DEALLOCATE PREPARE alterIfNotExists4;

-- Étape 5: Mettre à jour l'index idx_entity pour inclure programmation_id
-- Supprimer l'ancien index
SET @preparedStatement5 = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (INDEX_NAME = 'idx_entity')
  ) > 0,
  'ALTER TABLE `convention_documents`
   DROP INDEX `idx_entity`;',
  'SELECT "L\'index idx_entity n\'existe pas." AS message;'
));
PREPARE alterIfNotExists5 FROM @preparedStatement5;
EXECUTE alterIfNotExists5;
DEALLOCATE PREPARE alterIfNotExists5;

-- Recréer l'index avec programmation_id
SET @preparedStatement6 = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (INDEX_NAME = 'idx_entity')
  ) > 0,
  'SELECT "L\'index idx_entity existe déjà." AS message;',
  'ALTER TABLE `convention_documents`
   ADD INDEX `idx_entity` (`entity_type` ASC, `convention_cadre_id` ASC, `convention_specifique_id` ASC, `convention_id` ASC, `programmation_id` ASC, `avenant_id` ASC) USING BTREE;'
));
PREPARE alterIfNotExists6 FROM @preparedStatement6;
EXECUTE alterIfNotExists6;
DEALLOCATE PREPARE alterIfNotExists6;

-- Message de confirmation
SELECT "Migration 063: Ajout de programmation_id à convention_documents terminée avec succès." AS message;

