-- Migration: Création de la table pour les observations multiples des indicateurs de performance
-- Date: 2025-01-XX
-- Description: Crée la table pour stocker plusieurs observations avec dates pour chaque indicateur de performance

SET @dbname = DATABASE();
SET @tablename = 'convention_indicateur_observations';

-- Vérifier si la table existe déjà avant de la créer
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
  ) > 0,
  'SELECT "La table convention_indicateur_observations existe déjà." AS message;',
  CONCAT('CREATE TABLE `', @tablename, '` (
    `id` int NOT NULL AUTO_INCREMENT,
    `indicateur_id` int NOT NULL COMMENT "ID de l\'indicateur de performance",
    `observation` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT "Texte de l\'observation",
    `date_observation` date NULL DEFAULT NULL COMMENT "Date de l\'observation",
    `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,
    INDEX `idx_indicateur`(`indicateur_id` ASC) USING BTREE,
    INDEX `idx_date`(`date_observation` ASC) USING BTREE,
    CONSTRAINT `convention_indicateur_observations_ibfk_1` FOREIGN KEY (`indicateur_id`) REFERENCES `convention_indicateurs_performance` (`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 010: Création de la table convention_indicateur_observations terminée avec succès." AS message;

