-- Migration: Ajout de la table convention_commissions_physiques
-- Date: 2025-01-XX
-- Description: Crée la table convention_commissions_physiques pour le suivi physique des conventions visées.

SET @dbname = DATABASE();
SET @tablename = 'convention_commissions_physiques';
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
  ) > 0,
  'SELECT "La table convention_commissions_physiques existe déjà." AS message;',
  CONCAT('CREATE TABLE `', @tablename, '` (
    `id` int NOT NULL AUTO_INCREMENT,
    `convention_id` int NOT NULL COMMENT "ID de la convention",
    `numero_commission` int NOT NULL COMMENT "Numéro de la commission (1, 2, 3, etc.)",
    `date_commission` DATE NULL DEFAULT NULL COMMENT "Date de la commission",
    `demarre` tinyint(1) NOT NULL DEFAULT 0 COMMENT "Démarré (1) ou non démarré (0)",
    `taux_realisation_physique` DECIMAL(5, 2) NULL DEFAULT NULL COMMENT "Taux de réalisation physique en pourcentage (seulement si démarré)",
    `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
    `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`) USING BTREE,
    UNIQUE INDEX `unique_convention_commission`(`convention_id` ASC, `numero_commission` ASC) USING BTREE,
    INDEX `idx_convention`(`convention_id` ASC) USING BTREE,
    CONSTRAINT `convention_commissions_physiques_ibfk_1` FOREIGN KEY (`convention_id`) REFERENCES `conventions` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
  ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;')
));
PREPARE createIfNotExists FROM @preparedStatement;
EXECUTE createIfNotExists;
DEALLOCATE PREPARE createIfNotExists;

-- Message de confirmation
SELECT "Migration 014: Création de la table convention_commissions_physiques terminée avec succès." AS message;

