-- ============================================================================
-- Relances automatiques d'impayés : historique des exécutions.
-- À exécuter UNE FOIS sur la base du serveur (phpMyAdmin ou mysql), après sauvegarde. Idempotent : sans effet si la table existe déjà.
-- Compatible MySQL 8 et MariaDB >= 10.3. Structure uniquement : les réglages (heure, niveaux, plafond…) sont créés par l'application
-- dans n_parametres au premier enregistrement (Recouvrement › Relances automatiques) ; rien à insérer ici.
-- ============================================================================

SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `n_relances_executions` (
  `rex_id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `rex_debut` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `rex_fin` timestamp NULL DEFAULT NULL,
  `rex_declencheur` varchar(12) COLLATE utf8mb4_unicode_ci NOT NULL,
  `rex_statut` varchar(12) COLLATE utf8mb4_unicode_ci NOT NULL,
  `rex_motif` varchar(160) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `rex_propositions` int unsigned NOT NULL DEFAULT '0',
  `rex_envoyees` int unsigned NOT NULL DEFAULT '0',
  `rex_plafond` int unsigned NOT NULL DEFAULT '0',
  `rex_manuelles` int unsigned NOT NULL DEFAULT '0',
  `rex_sans_adresse` int unsigned NOT NULL DEFAULT '0',
  `rex_echecs` int unsigned NOT NULL DEFAULT '0',
  `rex_jour` date DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `created_by` bigint unsigned DEFAULT NULL,
  PRIMARY KEY (`rex_id`),
  KEY `idx_rex_jour` (`rex_jour`, `rex_declencheur`),
  KEY `n_relances_executions_created_by_index` (`created_by`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
