-- Création de la base de données geo_app si elle n'existe pas
DROP DATABASE IF EXISTS `geo_app`;
CREATE DATABASE IF NOT EXISTS `geo_app` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Création de l'utilisateur et attribution des droits
CREATE USER IF NOT EXISTS 'geo_app_user'@'localhost' IDENTIFIED BY 'QO:96df*?k{4W6m';
GRANT SELECT, INSERT, UPDATE, DELETE ON `geo_app`.* TO 'geo_app_user'@'localhost';
FLUSH PRIVILEGES;

USE geo_app;

--
-- Table structure for table `email_counter`
--

DROP TABLE IF EXISTS `email_counter`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `email_counter` (
  `id` int unsigned NOT NULL DEFAULT '1',
  `hour_start` timestamp NULL DEFAULT NULL,
  `count` int unsigned DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `x_devises`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `x_devises` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `code` varchar(3) DEFAULT NULL,
  `symbole` varchar(6) DEFAULT NULL,
  `libelle` varchar(45) DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `x_entites_types`
--

DROP TABLE IF EXISTS `x_entites_types`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `x_entites_types` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `libelle` varchar(45) DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `x_types_passages`
--

DROP TABLE IF EXISTS `x_types_passages`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `x_types_passages` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `libelle` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `color_button` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `color_mark` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `color_table` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `chk_active` tinyint(1) unsigned NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `x_types_reglements`
--

DROP TABLE IF EXISTS `x_types_reglements`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `x_types_reglements` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `libelle` varchar(45) DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `x_users_roles`
--

DROP TABLE IF EXISTS `x_users_roles`;

CREATE TABLE `x_users_roles` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `libelle` varchar(45) DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Les différents rôles des utilisateurs';
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `x_users_titres`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `x_users_titres` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `libelle` varchar(45) DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Les différents titres des utilisateurs';

DROP TABLE IF EXISTS `x_pays`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `x_pays` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `code` varchar(3) DEFAULT NULL,
  `fk_continent` int unsigned DEFAULT NULL,
  `fk_devise` int unsigned DEFAULT '1',
  `libelle` varchar(45) DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  CONSTRAINT `x_pays_ibfk_1` FOREIGN KEY (`fk_devise`) REFERENCES `x_devises` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Table des pays avec leurs codes';
/*!40101 SET character_set_client = @saved_cs_client */;


DROP TABLE IF EXISTS `x_regions`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `x_regions` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_pays` int unsigned DEFAULT '1',
  `libelle` varchar(45) DEFAULT NULL,
  `libelle_long` varchar(45) DEFAULT NULL,
  `table_osm` varchar(45) DEFAULT NULL,
  `departements` varchar(45) DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  CONSTRAINT `x_regions_ibfk_1` FOREIGN KEY (`fk_pays`) REFERENCES `x_pays` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `x_departements`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `x_departements` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `code` varchar(3) DEFAULT NULL,
  `fk_region` int unsigned DEFAULT '1',
  `libelle` varchar(45) DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  CONSTRAINT `x_departements_ibfk_1` FOREIGN KEY (`fk_region`) REFERENCES `x_regions` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=105 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `entites`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `entites` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `encrypted_name` varchar(255) DEFAULT NULL,
  `adresse1` varchar(45) DEFAULT '',
  `adresse2` varchar(45) DEFAULT '',
  `code_postal` varchar(5) DEFAULT '',
  `ville` varchar(45) DEFAULT '',
  `fk_region` int unsigned DEFAULT NULL,
  `fk_type` int unsigned DEFAULT '1',
  `encrypted_phone` varchar(128) DEFAULT '',
  `encrypted_mobile` varchar(128) DEFAULT '',
  `encrypted_email` varchar(255) DEFAULT '',
  `gps_lat` varchar(20) NOT NULL DEFAULT '',
  `gps_lng` varchar(20) NOT NULL DEFAULT '',
  `encrypted_stripe_id` varchar(255) DEFAULT '',
  `encrypted_iban` varchar(255) DEFAULT '',
  `encrypted_bic` varchar(128) DEFAULT '',
  `chk_demo` tinyint(1) unsigned DEFAULT '1',
  `chk_mdp_manuel` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT 'Gestion des mots de passe manuelle O/N',
  `chk_copie_mail_recu` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `chk_accept_sms` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
  `fk_user_creat` int unsigned DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Date de modification',
  `fk_user_modif` int unsigned DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`),
  CONSTRAINT `entites_ibfk_1` FOREIGN KEY (`fk_region`) REFERENCES `x_regions` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT `entites_ibfk_2` FOREIGN KEY (`fk_type`) REFERENCES `x_entites_types` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `x_villes`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `x_villes` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_departement` int unsigned DEFAULT '1',
  `libelle` varchar(65) DEFAULT NULL,
  `cp` varchar(5) DEFAULT NULL,
  `code_insee` varchar(5) DEFAULT NULL,
  `departement` varchar(65) DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  CONSTRAINT `x_villes_ibfk_1` FOREIGN KEY (`fk_departement`) REFERENCES `x_departements` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `users` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_entite` int unsigned DEFAULT '1',
  `fk_role` int unsigned DEFAULT '1',
  `fk_titre` int unsigned DEFAULT '1',
  `encrypted_name` varchar(255) DEFAULT NULL,
  `first_name` varchar(45) DEFAULT NULL,
  `sect_name` varchar(60) DEFAULT '',
  `encrypted_user_name` varchar(128) DEFAULT '',
  `user_pass_hash` varchar(60) DEFAULT NULL,
  `encrypted_phone` varchar(128) DEFAULT NULL,
  `encrypted_mobile` varchar(128) DEFAULT NULL,
  `encrypted_email` varchar(255) DEFAULT '',
  `chk_alert_email` tinyint(1) unsigned DEFAULT '1',
  `chk_suivi` tinyint(1) unsigned DEFAULT '0',
  `date_naissance` date DEFAULT NULL,
  `date_embauche` date DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
  `fk_user_creat` int unsigned DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Date de modification',
  `fk_user_modif` int unsigned DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `fk_entite` (`fk_entite`),
  KEY `username` (`encrypted_user_name`),
  CONSTRAINT `users_ibfk_1` FOREIGN KEY (`fk_entite`) REFERENCES `entites` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT `users_ibfk_2` FOREIGN KEY (`fk_role`) REFERENCES `x_users_roles` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT `users_ibfk_3` FOREIGN KEY (`fk_titre`) REFERENCES `x_users_titres` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `operations`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `operations` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_entite` int unsigned NOT NULL DEFAULT '1',
  `libelle` varchar(75) NOT NULL DEFAULT '',
  `date_deb` date NOT NULL DEFAULT '0000-00-00',
  `date_fin` date NOT NULL DEFAULT '0000-00-00',
  `chk_distinct_sectors` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
  `fk_user_creat` int unsigned NOT NULL DEFAULT '0',
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Date de modification',
  `fk_user_modif` int unsigned NOT NULL DEFAULT '0',
  `chk_active` tinyint(1) unsigned NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `fk_entite` (`fk_entite`),
  KEY `date_deb` (`date_deb`),
  CONSTRAINT `operations_ibfk_1` FOREIGN KEY (`fk_entite`) REFERENCES `entites` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;


DROP TABLE IF EXISTS `ope_sectors`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `ope_sectors` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_operation` int unsigned NOT NULL DEFAULT '0',
  `fk_old_sector` int unsigned NOT NULL DEFAULT '0',
  `libelle` varchar(75) NOT NULL DEFAULT '',
  `sector` text NOT NULL DEFAULT '',
  `color` varchar(7) NOT NULL DEFAULT '#4B77BE',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
  `fk_user_creat` int unsigned NOT NULL DEFAULT '0',
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Date de modification',
  `fk_user_modif` int unsigned NOT NULL DEFAULT '0',
  `chk_active` tinyint(1) unsigned NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`),
  KEY `fk_operation` (`fk_operation`),
  CONSTRAINT `ope_sectors_ibfk_1` FOREIGN KEY (`fk_operation`) REFERENCES `operations` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


DROP TABLE IF EXISTS `ope_users`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `ope_users` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_operation` int unsigned NOT NULL DEFAULT '0',
  `fk_user` int unsigned NOT NULL DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
  `fk_user_creat` int unsigned DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Date de modification',
  `fk_user_modif` int unsigned DEFAULT NULL,
  `chk_active` tinyint(1) unsigned NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  CONSTRAINT `ope_users_ibfk_1` FOREIGN KEY (`fk_operation`) REFERENCES `operations` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT `ope_users_ibfk_2` FOREIGN KEY (`fk_user`) REFERENCES `users` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `email_queue`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `email_queue` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_pass` int unsigned NOT NULL DEFAULT '0',
  `to_email` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `subject` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `body` text COLLATE utf8mb4_unicode_ci,
  `headers` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `status` enum('pending','sent','failed') COLLATE utf8mb4_unicode_ci DEFAULT 'pending',
  `attempts` int unsigned DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `ope_users_sectors`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `ope_users_sectors` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_operation` int unsigned NOT NULL DEFAULT '0',
  `fk_user` int unsigned NOT NULL DEFAULT '0',
  `fk_sector` int unsigned NOT NULL DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
  `fk_user_creat` int unsigned NOT NULL DEFAULT '0',
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Date de modification',
  `fk_user_modif` int unsigned DEFAULT NULL,
  `chk_active` tinyint(1) unsigned DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`),
  KEY `fk_operation` (`fk_operation`),
  KEY `fk_user` (`fk_user`),
  KEY `fk_sector` (`fk_sector`),
  CONSTRAINT `ope_users_sectors_ibfk_1` FOREIGN KEY (`fk_operation`) REFERENCES `operations` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT `ope_users_sectors_ibfk_2` FOREIGN KEY (`fk_user`) REFERENCES `users` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT `ope_users_sectors_ibfk_3` FOREIGN KEY (`fk_sector`) REFERENCES `ope_sectors` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `ope_users_suivis`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `ope_users_suivis` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_operation` int unsigned NOT NULL DEFAULT '0',
  `fk_user` int unsigned NOT NULL DEFAULT '0',
  `date_suivi` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date du suivi',
  `gps_lat` varchar(20) NOT NULL DEFAULT '',
  `gps_lng` varchar(20) NOT NULL DEFAULT '',
  `vitesse` varchar(20) NOT NULL DEFAULT '',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
  `fk_user_creat` int unsigned NOT NULL DEFAULT '0',
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Date de modification',
  `fk_user_modif` int unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `sectors_adresses`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `sectors_adresses` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_adresse` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'adresses.cp??.id',
  `osm_id` int unsigned NOT NULL DEFAULT '0',
  `fk_sector` int unsigned NOT NULL DEFAULT '0',
  `osm_name` varchar(50) NOT NULL DEFAULT '',
  `numero` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `rue_bis` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `rue` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `cp` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `ville` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `gps_lat` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `gps_lng` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `osm_date_creat` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Date de modification',
  PRIMARY KEY (`id`),
  KEY `sectors_adresses_fk_sector_index` (`fk_sector`),
  KEY `sectors_adresses_numero_index` (`numero`),
  KEY `sectors_adresses_rue_index` (`rue`),
  KEY `sectors_adresses_ville_index` (`ville`),
  CONSTRAINT `sectors_adresses_ibfk_1` FOREIGN KEY (`fk_sector`) REFERENCES `ope_sectors` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DROP TABLE IF EXISTS `ope_pass`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `ope_pass` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_operation` int unsigned NOT NULL DEFAULT '0',
  `fk_sector` int unsigned DEFAULT '0',
  `fk_user` int unsigned NOT NULL DEFAULT '0',
  `fk_adresse` varchar(25) DEFAULT '' COMMENT 'adresses.cp??.id',
  `passed_at` timestamp NULL DEFAULT NULL COMMENT 'Date du passage',
  `fk_type` int unsigned DEFAULT '0',
  `numero` varchar(10) NOT NULL DEFAULT '',
  `rue` varchar(75) NOT NULL DEFAULT '',
  `rue_bis` varchar(1) NOT NULL DEFAULT '',
  `ville` varchar(75) NOT NULL DEFAULT '',
  `fk_habitat` int unsigned DEFAULT '1',
  `appt` varchar(5) DEFAULT '',
  `niveau` varchar(5) DEFAULT '',
  `residence` varchar(75) DEFAULT '',
  `gps_lat` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `gps_lng` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `encrypted_name` varchar(255) NOT NULL DEFAULT '',
  `montant` decimal(7,2) NOT NULL DEFAULT '0.00',
  `fk_type_reglement` int unsigned DEFAULT '1',
  `remarque` text DEFAULT '',
  `encrypted_email` varchar(255) DEFAULT '',
  `nom_recu` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `date_recu` timestamp NULL DEFAULT NULL COMMENT 'Date de réception',
  `date_creat_recu` timestamp NULL DEFAULT NULL COMMENT 'Date de création du reçu',
  `date_sent_recu` timestamp NULL DEFAULT NULL COMMENT 'Date envoi du reçu',
  `email_erreur` varchar(30) DEFAULT '',
  `chk_email_sent` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `encrypted_phone` varchar(128) NOT NULL DEFAULT '',
  `chk_striped` tinyint(1) unsigned DEFAULT '0',
  `docremis` tinyint(1) unsigned DEFAULT '0',
  `date_repasser` timestamp NULL DEFAULT NULL COMMENT 'Date prévue pour repasser',
  `nb_passages` int DEFAULT '1' COMMENT 'Nb passages pour les a repasser',
  `chk_gps_maj` tinyint(1) unsigned DEFAULT '0',
  `chk_map_create` tinyint(1) unsigned DEFAULT '0',
  `chk_mobile` tinyint(1) unsigned DEFAULT '0',
  `chk_synchro` tinyint(1) unsigned DEFAULT '1' COMMENT 'chk synchro entre web et appli',
  `chk_api_adresse` tinyint(1) unsigned DEFAULT '0',
  `chk_maj_adresse` tinyint(1) unsigned DEFAULT '0',
  `anomalie` tinyint(1) unsigned DEFAULT '0',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
  `fk_user_creat` int unsigned DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'Date de modification',
  `fk_user_modif` int unsigned DEFAULT NULL,
  `chk_active` tinyint(1) unsigned NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `fk_operation` (`fk_operation`),
  KEY `fk_sector` (`fk_sector`),
  KEY `fk_user` (`fk_user`),
  KEY `fk_type` (`fk_type`),
  KEY `fk_type_reglement` (`fk_type_reglement`),
  KEY `email` (`email`),
  CONSTRAINT `ope_pass_ibfk_1` FOREIGN KEY (`fk_operation`) REFERENCES `operations` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT `ope_pass_ibfk_2` FOREIGN KEY (`fk_sector`) REFERENCES `ope_sectors` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT `ope_pass_ibfk_3` FOREIGN KEY (`fk_user`) REFERENCES `users` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT `ope_pass_ibfk_4` FOREIGN KEY (`fk_type_reglement`) REFERENCES `x_types_reglements` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `ope_pass_histo`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `ope_pass_histo` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `fk_pass` int unsigned NOT NULL DEFAULT '0',
  `date_histo` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date historique',
  `sujet` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `remarque` varchar(250) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `ope_pass_histo_fk_pass_IDX` (`fk_pass`) USING BTREE,
  KEY `ope_pass_histo_date_histo_IDX` (`date_histo`) USING BTREE,
  CONSTRAINT `ope_pass_histo_ibfk_1` FOREIGN KEY (`fk_pass`) REFERENCES `ope_pass` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS `medias`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `medias` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `support` varchar(45) NOT NULL DEFAULT '',
  `support_id` int unsigned NOT NULL DEFAULT '0',
  `fichier` varchar(250) NOT NULL DEFAULT '',
  `description` varchar(100) NOT NULL DEFAULT '',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `fk_user_creat` int unsigned NOT NULL DEFAULT '0',
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `fk_user_modif` int unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

-- Création des tables pour le système de chat
DROP TABLE IF EXISTS `chat_rooms`;
-- Table des salles de discussion
CREATE TABLE chat_rooms (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    type ENUM('privee', 'groupe', 'liste_diffusion') NOT NULL,
    date_creation timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
    fk_user INT UNSIGNED NOT NULL,
    fk_entite INT UNSIGNED,
    statut ENUM('active', 'archive') NOT NULL DEFAULT 'active',
    description TEXT,
    INDEX idx_user (fk_user),
    INDEX idx_entite (fk_entite)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


DROP TABLE IF EXISTS `chat_participants`;
-- Table des participants aux salles de discussion
CREATE TABLE chat_participants (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_room INT UNSIGNED NOT NULL,
    id_user INT UNSIGNED NOT NULL,
    role ENUM('administrateur', 'participant', 'en_lecture_seule') NOT NULL DEFAULT 'participant',
    date_ajout timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date ajout',
    notification_activee BOOLEAN NOT NULL DEFAULT TRUE,
    INDEX idx_room (id_room),
    INDEX idx_user (id_user),
    CONSTRAINT uc_room_user UNIQUE (id_room, id_user),
    FOREIGN KEY (id_room) REFERENCES chat_rooms(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DROP TABLE IF EXISTS `chat_messages`;
-- Table des messages
CREATE TABLE chat_messages (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    fk_room INT UNSIGNED NOT NULL,
    fk_user INT UNSIGNED NOT NULL,
    content TEXT,
    date_sent timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date envoi',
    type ENUM('texte', 'media', 'systeme') NOT NULL DEFAULT 'texte',
    statut ENUM('envoye', 'livre', 'lu') NOT NULL DEFAULT 'envoye',
    INDEX idx_room (fk_room),
    INDEX idx_user (fk_user),
    INDEX idx_date (date_sent),
    FOREIGN KEY (fk_room) REFERENCES chat_rooms(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DROP TABLE IF EXISTS `chat_listes_diffusion`;
-- Table des listes de diffusion
CREATE TABLE chat_listes_diffusion (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    fk_room INT UNSIGNED NOT NULL,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    fk_user INT UNSIGNED NOT NULL,
    date_creation timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
    INDEX idx_room (fk_room),
    INDEX idx_user (fk_user),
    FOREIGN KEY (fk_room) REFERENCES chat_rooms(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DROP TABLE IF EXISTS `chat_read_messages`;
-- Table pour suivre la lecture des messages
CREATE TABLE chat_read_messages (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    fk_message INT UNSIGNED NOT NULL,
    fk_user INT UNSIGNED NOT NULL,
    date_read timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de lecture',
    INDEX idx_message (fk_message),
    INDEX idx_user (fk_user),
    CONSTRAINT uc_message_user UNIQUE (fk_message, fk_user),
    FOREIGN KEY (fk_message) REFERENCES chat_messages(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DROP TABLE IF EXISTS `chat_notifications`;
-- Table des notifications
CREATE TABLE chat_notifications (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    fk_user INT UNSIGNED NOT NULL,
    fk_message INT UNSIGNED,
    fk_room INT UNSIGNED,
    type VARCHAR(50) NOT NULL,
    contenu TEXT,
    date_creation timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Date de création',
    date_lecture timestamp NULL DEFAULT NULL COMMENT 'Date de lecture',
    statut ENUM('non_lue', 'lue') NOT NULL DEFAULT 'non_lue',
    INDEX idx_user (fk_user),
    INDEX idx_message (fk_message),
    INDEX idx_room (fk_room),
    FOREIGN KEY (fk_message) REFERENCES chat_messages(id) ON DELETE SET NULL,
    FOREIGN KEY (fk_room) REFERENCES chat_rooms(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DROP TABLE IF EXISTS `z_params`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `params` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `libelle` varchar(35) NOT NULL DEFAULT '',
  `valeur` varchar(255) NOT NULL DEFAULT '',
  `aide` varchar(150) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;


DROP TABLE IF EXISTS `z_sessions`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `z_sessions` (
  `sid` text NOT NULL,
  `fk_user` int NOT NULL,
  `role` varchar(10) DEFAULT NULL,
  `date_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `ip` varchar(50) NOT NULL,
  `browser` varchar(150) NOT NULL,
  `data` mediumtext
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
