-- Migration: Ajout de la table convention_commission_mediatheque
-- Date: 2025-01-XX
-- Description: Crée la table convention_commission_mediatheque pour stocker les documents de la médiathèque liés aux commissions physiques.

SET @dbname = DATABASE();
SET @tablename = 'convention_commission_mediatheque';
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
  ) > 0,
  'SELECT "La table convention_commission_mediatheque existe déjà." AS message;',
  CONCAT('CREATE TABLE `', @tablename, '` (
    `id` int NOT NULL AUTO_INCREMENT,
    `commission_id` int NOT NULL COMMENT "ID de la commission",
    `nom_document` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT "Nom du document",
    `chemin_fichier` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT "Chemin du fichier",
    `taille_fichier` int NULL DEFAULT NULL COMMENT "Taille du fichier en octets",
    `type_mime` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT "Type MIME du fichier",
    `uploaded_by` int NOT NULL COMMENT "ID de l\'utilisateur qui a uploadé",
    `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_commission`(`commission_id` ASC) USING BTREE,
    INDEX `idx_uploaded_by`(`uploaded_by` ASC) USING BTREE,
    CONSTRAINT `convention_commission_mediatheque_ibfk_1` FOREIGN KEY (`commission_id`) REFERENCES `convention_commissions_physiques` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT,
    CONSTRAINT `convention_commission_mediatheque_ibfk_2` FOREIGN KEY (`uploaded_by`) REFERENCES `users` (`id`) ON DELETE RESTRICT 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 016: Création de la table convention_commission_mediatheque terminée avec succès." AS message;

