-- Stages paramédicaux : tables, colonnes et données de référence. À exécuter sur polysco_db (MySQL/MariaDB).
-- 1) Colonnes ajoutées aux tables existantes
ALTER TABLE n_entreprises ADD COLUMN ent_type ENUM('entreprise','etablissement_sante') NOT NULL DEFAULT 'entreprise' AFTER ent_raison_sociale;
ALTER TABLE n_stages MODIFY stg_type ENUM('initiation','technique','pfa','pfe','autre','clinique') NOT NULL;
-- 2) Nouvelles tables (n_stage_types_service, n_stage_services, n_stage_exigences, n_stage_rotations) avec leurs données

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `n_stage_types_service` (
  `tsv_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `tsv_nom` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `tsv_ordre` smallint(5) unsigned NOT NULL DEFAULT '0',
  `tsv_est_actif` tinyint(1) NOT NULL DEFAULT '1',
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`tsv_id`),
  UNIQUE KEY `n_stage_types_service_tsv_nom_unique` (`tsv_nom`),
  KEY `idx_created_by` (`created_by`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

LOCK TABLES `n_stage_types_service` WRITE;
/*!40000 ALTER TABLE `n_stage_types_service` DISABLE KEYS */;
INSERT INTO `n_stage_types_service` VALUES (1,'Urgences',1,1,'2026-10-09 12:26:25','2026-10-09 12:26:25',NULL),(2,'Médecine',2,1,'2026-10-09 12:26:26','2026-10-09 12:26:26',NULL),(3,'Chirurgie',3,1,'2026-10-09 12:26:26','2026-10-09 12:26:26',NULL),(4,'Pédiatrie',4,1,'2026-10-09 12:26:26','2026-10-09 12:26:26',NULL),(5,'Maternité',5,1,'2026-10-09 12:26:26','2026-10-09 12:26:26',NULL),(6,'Bloc opératoire',6,1,'2026-10-09 12:26:26','2026-10-09 12:26:26',NULL),(7,'Réanimation',7,1,'2026-10-09 12:26:26','2026-10-09 12:26:26',NULL),(8,'Consultations externes',8,1,'2026-10-09 12:26:26','2026-10-09 12:26:26',NULL),(9,'Radiologie et laboratoire',9,1,'2026-10-09 12:26:26','2026-10-09 12:26:26',NULL),(10,'Santé mentale',10,1,'2026-10-09 12:26:26','2026-10-09 12:26:26',NULL);
/*!40000 ALTER TABLE `n_stage_types_service` ENABLE KEYS */;
UNLOCK TABLES;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `n_stage_services` (
  `ssv_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `ssv_ent_id` bigint(20) unsigned NOT NULL,
  `ssv_type_id` bigint(20) unsigned NOT NULL,
  `ssv_nom` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL,
  `ssv_capacite` smallint(5) unsigned NOT NULL DEFAULT '2',
  `ssv_maitre_nom` varchar(150) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `ssv_maitre_tel` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `ssv_est_actif` tinyint(1) NOT NULL DEFAULT '1',
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`ssv_id`),
  KEY `n_stage_services_ssv_type_id_foreign` (`ssv_type_id`),
  KEY `n_stage_services_created_by_foreign` (`created_by`),
  KEY `n_stage_services_ssv_ent_id_ssv_type_id_index` (`ssv_ent_id`,`ssv_type_id`),
  CONSTRAINT `n_stage_services_created_by_foreign` FOREIGN KEY (`created_by`) REFERENCES `n_utilisateurs` (`user_id`) ON DELETE SET NULL,
  CONSTRAINT `n_stage_services_ssv_ent_id_foreign` FOREIGN KEY (`ssv_ent_id`) REFERENCES `n_entreprises` (`ent_id`) ON DELETE CASCADE,
  CONSTRAINT `n_stage_services_ssv_type_id_foreign` FOREIGN KEY (`ssv_type_id`) REFERENCES `n_stage_types_service` (`tsv_id`) ON DELETE RESTRICT
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

LOCK TABLES `n_stage_services` WRITE;
/*!40000 ALTER TABLE `n_stage_services` DISABLE KEYS */;
INSERT INTO `n_stage_services` VALUES (1,13,1,'Urgences adultes',6,'Dr Alaoui','0661000000',1,'2026-10-09 12:27:00','2026-10-09 13:18:01',NULL,670),(2,13,4,'Pédiatrie A',3,'Mme Benjelloun','0661000000',1,'2026-10-09 12:27:01','2026-10-09 12:27:01',NULL,670),(3,13,5,'Maternité',2,'Mme Tazi','0661000000',1,'2026-10-09 12:27:01','2026-10-09 12:27:01',NULL,670),(4,14,3,'Chirurgie générale',3,'Cdt Berrada','0661000000',1,'2026-10-09 12:27:01','2026-10-09 12:27:01',NULL,670),(5,14,1,'Urgences',6,'Dr Kettani','0661000000',1,'2026-10-09 12:27:01','2026-10-09 13:18:01',NULL,670),(6,14,2,'Médecine interne',4,'Dr Fassi','0661000000',1,'2026-10-09 12:27:01','2026-10-09 13:17:44',NULL,670),(7,15,4,'Pédiatrie',2,'Dr Hajji','0661000000',1,'2026-10-09 12:27:02','2026-10-09 12:27:02',NULL,670),(8,15,5,'Maternité',2,'Mme Naciri','0661000000',1,'2026-10-09 12:27:02','2026-10-09 12:27:02',NULL,670);
/*!40000 ALTER TABLE `n_stage_services` ENABLE KEYS */;
UNLOCK TABLES;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `n_stage_exigences` (
  `sex_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `sex_formation_id` bigint(20) unsigned NOT NULL,
  `sex_niveau` tinyint(3) unsigned NOT NULL,
  `sex_type_id` bigint(20) unsigned NOT NULL,
  `sex_heures_min` smallint(5) unsigned NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`sex_id`),
  UNIQUE KEY `uq_stage_exigence` (`sex_formation_id`,`sex_niveau`,`sex_type_id`),
  KEY `n_stage_exigences_sex_type_id_foreign` (`sex_type_id`),
  KEY `idx_created_by` (`created_by`),
  CONSTRAINT `n_stage_exigences_sex_formation_id_foreign` FOREIGN KEY (`sex_formation_id`) REFERENCES `n_formations` (`for_id`) ON DELETE CASCADE,
  CONSTRAINT `n_stage_exigences_sex_type_id_foreign` FOREIGN KEY (`sex_type_id`) REFERENCES `n_stage_types_service` (`tsv_id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

LOCK TABLES `n_stage_exigences` WRITE;
/*!40000 ALTER TABLE `n_stage_exigences` DISABLE KEYS */;
INSERT INTO `n_stage_exigences` VALUES (1,2,2,1,80,'2026-10-09 12:27:03','2026-10-09 12:27:03',670),(2,2,2,4,80,'2026-10-09 12:27:03','2026-10-09 12:27:03',670),(3,2,2,5,60,'2026-10-09 12:27:03','2026-10-09 12:27:03',670),(4,22,1,1,80,'2026-10-09 13:17:45','2026-10-09 13:17:45',670),(5,22,1,2,80,'2026-10-09 13:17:45','2026-10-09 13:17:45',670),(6,22,1,3,80,'2026-10-09 13:17:45','2026-10-09 13:17:45',670);
/*!40000 ALTER TABLE `n_stage_exigences` ENABLE KEYS */;
UNLOCK TABLES;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `n_stage_rotations` (
  `rot_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `rot_classe_id` bigint(20) unsigned NOT NULL,
  `rot_as_id` bigint(20) unsigned NOT NULL,
  `rot_libelle` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `rot_debut` date NOT NULL,
  `rot_fin` date NOT NULL,
  `rot_ordre` smallint(5) unsigned NOT NULL DEFAULT '1',
  `created_at` timestamp NULL DEFAULT (now()),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`rot_id`),
  KEY `n_stage_rotations_rot_as_id_foreign` (`rot_as_id`),
  KEY `n_stage_rotations_created_by_foreign` (`created_by`),
  KEY `n_stage_rotations_rot_classe_id_rot_as_id_rot_ordre_index` (`rot_classe_id`,`rot_as_id`,`rot_ordre`),
  CONSTRAINT `n_stage_rotations_created_by_foreign` FOREIGN KEY (`created_by`) REFERENCES `n_utilisateurs` (`user_id`) ON DELETE SET NULL,
  CONSTRAINT `n_stage_rotations_rot_as_id_foreign` FOREIGN KEY (`rot_as_id`) REFERENCES `n_annees_scolaires` (`as_id`),
  CONSTRAINT `n_stage_rotations_rot_classe_id_foreign` FOREIGN KEY (`rot_classe_id`) REFERENCES `n_classes` (`classe_id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

LOCK TABLES `n_stage_rotations` WRITE;
/*!40000 ALTER TABLE `n_stage_rotations` DISABLE KEYS */;
INSERT INTO `n_stage_rotations` VALUES (1,43,14,'Rotation 1','2026-11-02','2026-11-27',1,'2026-10-09 12:27:03','2026-10-09 12:27:03',670),(2,43,14,'Rotation 2','2026-11-30','2026-12-25',2,'2026-10-09 12:27:03','2026-10-09 12:27:03',670),(3,43,14,'Rotation 3','2026-12-28','2027-01-15',3,'2026-10-09 12:27:03','2026-10-09 12:27:03',670),(4,46,14,'Rotation 1','2026-11-02','2026-11-27',1,'2026-10-09 13:17:45','2026-10-09 13:17:45',670),(5,46,14,'Rotation 2','2026-11-30','2026-12-25',2,'2026-10-09 13:17:45','2026-10-09 13:17:45',670),(6,46,14,'Rotation 3','2026-12-28','2027-01-22',3,'2026-10-09 13:17:45','2026-10-09 13:17:45',670);
/*!40000 ALTER TABLE `n_stage_rotations` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;


-- 3) Liens de n_stages vers le service et la rotation
ALTER TABLE n_stages ADD COLUMN stg_service_id BIGINT UNSIGNED NULL AFTER stg_ent_id, ADD COLUMN stg_rotation_id BIGINT UNSIGNED NULL AFTER stg_service_id,
  ADD CONSTRAINT n_stages_stg_service_id_foreign FOREIGN KEY (stg_service_id) REFERENCES n_stage_services (ssv_id) ON DELETE SET NULL,
  ADD CONSTRAINT n_stages_stg_rotation_id_foreign FOREIGN KEY (stg_rotation_id) REFERENCES n_stage_rotations (rot_id) ON DELETE SET NULL,
  ADD INDEX n_stages_stg_rotation_id_stg_service_id_index (stg_rotation_id, stg_service_id);
