-- evaluationjaj v1.0 — Schéma MySQL
-- À exécuter une seule fois sur le serveur


-- ─────────────────────────────────────────
-- UTILISATEURS (admin, formateur, arbitre)
-- ─────────────────────────────────────────
CREATE TABLE users (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  email       VARCHAR(255) NOT NULL UNIQUE,
  password    VARCHAR(255) NOT NULL,
  role        ENUM('admin','formateur','arbitre') NOT NULL DEFAULT 'arbitre',
  active      BOOLEAN NOT NULL DEFAULT TRUE,
  created_at  DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at  DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- ─────────────────────────────────────────
-- SAISONS
-- ─────────────────────────────────────────
CREATE TABLE saisons (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  label       VARCHAR(20) NOT NULL UNIQUE,   -- ex: "2025-2026"
  active      BOOLEAN NOT NULL DEFAULT FALSE, -- une seule saison active à la fois
  created_at  DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ─────────────────────────────────────────
-- ARBITRES
-- ─────────────────────────────────────────
CREATE TABLE arbitres (
  id              INT AUTO_INCREMENT PRIMARY KEY,
  user_id         INT UNIQUE REFERENCES users(id) ON DELETE SET NULL,
  nom             VARCHAR(100) NOT NULL,
  prenom          VARCHAR(100) NOT NULL,
  sexe            ENUM('H','F') NOT NULL,
  date_naissance  DATE,
  binome          TINYINT,
  grade           VARCHAR(50),
  groupe          ENUM('T1','T2'),
  programme       VARCHAR(100),
  gesthand_id     VARCHAR(50),
  gesthand_num    VARCHAR(50),
  licence         VARCHAR(50),
  statut          VARCHAR(50),
  passport        VARCHAR(50),
  nom_club        VARCHAR(150),
  adresse         VARCHAR(255),
  cp              VARCHAR(10),
  ville           VARCHAR(100),
  mail_perso      VARCHAR(255),
  whatsapp        VARCHAR(20),
  commentaires    TEXT,
  active          BOOLEAN NOT NULL DEFAULT TRUE,
  created_at      DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at      DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- ─────────────────────────────────────────
-- COEFFICIENTS PAR SAISON
-- ─────────────────────────────────────────
CREATE TABLE coefficients (
  id              INT AUTO_INCREMENT PRIMARY KEY,
  saison_id       INT NOT NULL REFERENCES saisons(id),
  critere         VARCHAR(50) NOT NULL,
  coefficient     DECIMAL(5,2) NOT NULL DEFAULT 1,
  seuil_dispo     DECIMAL(5,2) DEFAULT 0.70, -- seuil % dispo → 100%
  seuil_webinaire DECIMAL(5,2) DEFAULT 0.70, -- seuil présence webinaire
  updated_by      INT REFERENCES users(id),
  updated_at      DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uq_saison_critere (saison_id, critere)
);

-- ─────────────────────────────────────────
-- DISPONIBILITÉS (une ligne par arbitre/jour/saison)
-- ─────────────────────────────────────────
CREATE TABLE disponibilites (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  arbitre_id  INT NOT NULL REFERENCES arbitres(id),
  saison_id   INT NOT NULL REFERENCES saisons(id),
  semestre    ENUM('S1','S2') NOT NULL,
  date_jour   DATE NOT NULL,
  valeur      DECIMAL(4,2) NOT NULL DEFAULT 1, -- 1=dispo, 0=indispo, -1=non concerné, ou fraction
  UNIQUE KEY uq_dispo (arbitre_id, saison_id, date_jour)
);

-- ─────────────────────────────────────────
-- QCM (résultats par arbitre)
-- ─────────────────────────────────────────
CREATE TABLE qcm (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  saison_id   INT NOT NULL REFERENCES saisons(id),
  arbitre_id  INT NOT NULL REFERENCES arbitres(id),
  type_qcm    ENUM('stage_rentree','qcm1','qcm2','qcm3','qcm4','stage_mi_saison','qcm5','qcm6','qcm7','qcm8') NOT NULL,
  note        DECIMAL(5,4),            -- valeur entre 0 et 1
  date_test   DATETIME,
  temps_rep   VARCHAR(20),
  structure   VARCHAR(255),
  imported_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_qcm (saison_id, arbitre_id, type_qcm)
);

-- ─────────────────────────────────────────
-- PRÉSENCES STAGES
-- ─────────────────────────────────────────
CREATE TABLE presences_stages (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  saison_id   INT NOT NULL REFERENCES saisons(id),
  arbitre_id  INT NOT NULL REFERENCES arbitres(id),
  type_stage  ENUM('rentree','mi_saison') NOT NULL,
  samedi      TINYINT DEFAULT 0,
  dimanche    TINYINT DEFAULT 0,
  total       DECIMAL(4,2),
  imported_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_stage (saison_id, arbitre_id, type_stage)
);

-- ─────────────────────────────────────────
-- PRÉSENCES WEBINAIRES
-- ─────────────────────────────────────────
CREATE TABLE presences_webinaires (
  id              INT AUTO_INCREMENT PRIMARY KEY,
  saison_id       INT NOT NULL REFERENCES saisons(id),
  arbitre_id      INT NOT NULL REFERENCES arbitres(id),
  semestre        ENUM('S1','S2') NOT NULL,
  numero_web      TINYINT NOT NULL,    -- 1 à 4
  duree_minutes   INT,
  note            DECIMAL(5,4),        -- calculée
  imported_at     DATETIME DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_web (saison_id, arbitre_id, semestre, numero_web)
);

-- ─────────────────────────────────────────
-- RETOURS SMS (auto-évaluation post-match)
-- ─────────────────────────────────────────
CREATE TABLE retours_sms (
  id              INT AUTO_INCREMENT PRIMARY KEY,
  saison_id       INT NOT NULL REFERENCES saisons(id),
  arbitre_id      INT NOT NULL REFERENCES arbitres(id),
  semestre        ENUM('S1','S2') NOT NULL,
  date_match      DATE,
  championnat     VARCHAR(255),
  equipe_rec      VARCHAR(255),
  equipe_vis      VARCHAR(255),
  note_a1         TINYINT,
  note_a2         TINYINT,
  note_b1         TINYINT,
  note_b2         TINYINT,
  note_b3         TINYINT,
  note_c1         TINYINT,
  note_c2         TINYINT,
  note_c3         TINYINT,
  difficulte      ENUM('Normal','Difficile','Très difficile'),
  raisons         TEXT,
  commentaire_match TEXT,
  points_reussis  TEXT,
  points_faibles  TEXT,
  commentaires    TEXT,
  imported_at     DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ─────────────────────────────────────────
-- RETOURS DE CONVOCATION (malus — plusieurs par arbitre/semestre)
-- ─────────────────────────────────────────
CREATE TABLE retours_convocation (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  saison_id   INT NOT NULL REFERENCES saisons(id),
  arbitre_id  INT NOT NULL REFERENCES arbitres(id),
  semestre    ENUM('S1','S2') NOT NULL,
  date_match  DATE,
  label       VARCHAR(255),           -- libellé libre ex: "Match du 12/10/2025"
  valeur      DECIMAL(4,2) NOT NULL,  -- -1, 0, 0.5 (non évalué)
  created_by  INT REFERENCES users(id),
  created_at  DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ─────────────────────────────────────────
-- SUIVIS (note manuelle par semestre)
-- ─────────────────────────────────────────
CREATE TABLE suivis (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  saison_id   INT NOT NULL REFERENCES saisons(id),
  arbitre_id  INT NOT NULL REFERENCES arbitres(id),
  semestre    ENUM('S1','S2') NOT NULL,
  note        DECIMAL(5,2),           -- 0 à 100 (%)
  created_by  INT REFERENCES users(id),
  updated_at  DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uq_suivi (saison_id, arbitre_id, semestre)
);

-- ─────────────────────────────────────────
-- JOURNAL DES MODIFICATIONS COEFFICIENTS
-- ─────────────────────────────────────────
CREATE TABLE coeff_log (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  saison_id   INT NOT NULL REFERENCES saisons(id),
  critere     VARCHAR(50) NOT NULL,
  ancienne_val DECIMAL(5,2),
  nouvelle_val DECIMAL(5,2),
  user_id     INT REFERENCES users(id),
  created_at  DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ─────────────────────────────────────────
-- SEED : admin par défaut
-- ─────────────────────────────────────────
-- Mot de passe : Admin1234! (à changer impérativement)
INSERT INTO users (email, password, role) VALUES
('admin@evaluationjaj.omnelya.fr', '$2a$12$LGfmYqbmjGjO1JJ2w3m7.OmqEK7n8xZQ3v2aP8kW9cF1dR4eH6oYG', 'admin');

-- Saison initiale
INSERT INTO saisons (label, active) VALUES ('2025-2026', TRUE);

-- Coefficients par défaut pour la saison 2025-2026
INSERT INTO coefficients (saison_id, critere, coefficient) VALUES
(1, 'retour_convocation',   10),
(1, 'dispo_s1',              5),
(1, 'dispo_s2',              5),
(1, 'presence_stage_rentree', 9),
(1, 'presence_stage_mi_saison', 7),
(1, 'presence_web_s1',       1),
(1, 'presence_web_s2',       1),
(1, 'qcm_stage_rentree',     3),
(1, 'qcm1',                  3),
(1, 'qcm2',                  3),
(1, 'qcm3',                  3),
(1, 'qcm4',                  3),
(1, 'qcm_stage_mi_saison',   3),
(1, 'qcm5',                  3),
(1, 'qcm6',                  3),
(1, 'qcm7',                  3),
(1, 'qcm8',                  3),
(1, 'retour_sms_s1',         2),
(1, 'retour_sms_s2',         2),
(1, 'suivis_s1',             8),
(1, 'suivis_s2',             8);
