-- IDEE-4 / IDEE-5 — Radar mail proto (CRM)
-- Tables pour l’écran /v4/radar. Moteur Gmail Push + Gemini : story suivante.
-- Exécuter sur la BD CRM de DEV (devcrmms1_dev), pas en prod.

CREATE TABLE IF NOT EXISTS radar_exceptions (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    kind ENUM('mailbox', 'sender') NOT NULL DEFAULT 'mailbox',
    value VARCHAR(190) NOT NULL,
    note VARCHAR(255) NOT NULL DEFAULT '',
    active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uq_radar_exc (kind, value)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS radar_threads (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    gmail_thread_id VARCHAR(64) NOT NULL,
    mailbox VARCHAR(190) NOT NULL DEFAULT '',
    subject VARCHAR(255) NOT NULL DEFAULT '',
    from_email VARCHAR(190) NOT NULL DEFAULT '',
    from_name VARCHAR(190) NOT NULL DEFAULT '',
    gmail_url VARCHAR(500) NOT NULL DEFAULT '',
    client_id INT UNSIGNED NULL DEFAULT NULL,
    projet_id INT UNSIGNED NULL DEFAULT NULL,
    proposed_client_no VARCHAR(32) NOT NULL DEFAULT '',
    status VARCHAR(32) NOT NULL DEFAULT 'a_classer',
    summary VARCHAR(500) NOT NULL DEFAULT '',
    confidence TINYINT UNSIGNED NOT NULL DEFAULT 0,
    is_example TINYINT(1) NOT NULL DEFAULT 0,
    received_at DATETIME NULL DEFAULT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uq_radar_thread (gmail_thread_id),
    KEY idx_radar_status (status),
    KEY idx_radar_received (received_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS radar_thread_skills (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    thread_id INT UNSIGNED NOT NULL,
    skill VARCHAR(32) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uq_radar_skill (thread_id, skill),
    KEY idx_radar_skill (skill)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS radar_rules (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    match_type ENUM('email', 'domain') NOT NULL DEFAULT 'email',
    match_value VARCHAR(190) NOT NULL,
    client_id INT UNSIGNED NULL DEFAULT NULL,
    projet_id INT UNSIGNED NULL DEFAULT NULL,
    skill VARCHAR(32) NOT NULL DEFAULT '',
    source VARCHAR(32) NOT NULL DEFAULT 'human',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uq_radar_rule (match_type, match_value, skill)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO radar_exceptions (kind, value, note, active)
SELECT 'mailbox', 'confirmation@ms1timing.com', 'Mails transactionnels inscription — ne pas ouvrir', 1
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM radar_exceptions
    WHERE kind = 'mailbox' AND value = 'confirmation@ms1timing.com'
);

INSERT INTO radar_threads (
    gmail_thread_id, mailbox, subject, from_email, from_name, gmail_url,
    proposed_client_no, status, summary, confidence, is_example, received_at
)
SELECT
    'proto-ex-1',
    'desjardins.lp@ms1timing.com',
    'Logos dossards + fichiers départ site',
    'marie@course-exemple.ca',
    'Marie Exemple',
    '',
    '1842',
    'a_classer',
    'Deux sujets dans le même fil : logos pour l’imprimeur, et fichiers à déposer pour le site d’inscription.',
    72,
    1,
    NOW()
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM radar_threads WHERE gmail_thread_id = 'proto-ex-1');

INSERT INTO radar_thread_skills (thread_id, skill)
SELECT t.id, 'dossards' FROM radar_threads t
WHERE t.gmail_thread_id = 'proto-ex-1'
  AND NOT EXISTS (
      SELECT 1 FROM radar_thread_skills s WHERE s.thread_id = t.id AND s.skill = 'dossards'
  );

INSERT INTO radar_thread_skills (thread_id, skill)
SELECT t.id, 'site' FROM radar_threads t
WHERE t.gmail_thread_id = 'proto-ex-1'
  AND NOT EXISTS (
      SELECT 1 FROM radar_thread_skills s WHERE s.thread_id = t.id AND s.skill = 'site'
  );

INSERT INTO radar_threads (
    gmail_thread_id, mailbox, subject, from_email, from_name, gmail_url,
    proposed_client_no, status, summary, confidence, is_example, received_at
)
SELECT
    'proto-ex-2',
    'info@ms1timing.com',
    'Question impression dossards',
    'nouveau@inconnu.example',
    'Contact inconnu',
    '',
    '',
    'a_mettre_dans_crm',
    'Adresse absente du CRM. Créer le contact / projet, puis relancer — le fil doit se recoller tout seul.',
    40,
    1,
    DATE_SUB(NOW(), INTERVAL 25 MINUTE)
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM radar_threads WHERE gmail_thread_id = 'proto-ex-2');

INSERT INTO radar_thread_skills (thread_id, skill)
SELECT t.id, 'dossards' FROM radar_threads t
WHERE t.gmail_thread_id = 'proto-ex-2'
  AND NOT EXISTS (
      SELECT 1 FROM radar_thread_skills s WHERE s.thread_id = t.id AND s.skill = 'dossards'
  );
