-- MSIN-4485 — Impression dossard PDF (fiche gestion) + droit hors kit total
-- Prérequis : inscriptions_eve_permissions (accès v2)
-- Inclut la colonne MSIN-4484 (perm_grants_all) si absente — safe si 4484 déjà joué
-- Notes : exécution UNIQUEMENT sur dev préprod ; autres env = Navicat structure + sync_static_db
-- Idempotent
--
-- Droit non propagé (perm_grants_all = 0) : activer via extra / matrice explicite
-- sur le compte voulu — pas via kit owner / total.

SET NAMES utf8mb4;

/* MSIN-4484 — colonne hors propagation kit total (si pas déjà exécuté) */
SET @col_exists := (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'inscriptions_eve_permissions'
    AND COLUMN_NAME = 'perm_grants_all'
);

SET @sql := IF(
  @col_exists = 0,
  'ALTER TABLE `inscriptions_eve_permissions`
     ADD COLUMN `perm_grants_all` tinyint(1) unsigned NOT NULL DEFAULT 1
       COMMENT ''MSIN-4484 — 1 = incluse dans kit total (grants_all) ; 0 = non propagée''
       AFTER `perm_actif`',
  'SELECT ''perm_grants_all already exists'' AS info'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

INSERT INTO `inscriptions_eve_permissions`
  (`perm_key`, `perm_group`, `perm_scope`, `perm_label_fr`, `perm_label_en`,
   `perm_description_fr`, `perm_description_en`, `perm_phase`, `perm_actif`,
   `perm_grants_all`, `perm_tri`)
VALUES
  ('inscriptions_gestion.dossard_print', 'inscriptions', 'action',
   'Gestion inscription : imprimer le dossard (PDF)',
   'Registration mgmt: print bib (PDF)',
   'Bouton Imprimer le dossard sur la fiche participant (PDF demi-lettre)',
   'Print bib button on participant sheet (half-letter PDF)',
   2, 1, 0, 119)
ON DUPLICATE KEY UPDATE
  `perm_group` = VALUES(`perm_group`),
  `perm_scope` = VALUES(`perm_scope`),
  `perm_label_fr` = VALUES(`perm_label_fr`),
  `perm_label_en` = VALUES(`perm_label_en`),
  `perm_description_fr` = VALUES(`perm_description_fr`),
  `perm_description_en` = VALUES(`perm_description_en`),
  `perm_phase` = VALUES(`perm_phase`),
  `perm_actif` = 1,
  `perm_grants_all` = 0,
  `perm_tri` = VALUES(`perm_tri`);

/* Ne pas laisser ce droit dans un kit par accident (hors propagation). */
DELETE FROM `inscriptions_eve_role_permissions`
WHERE `perm_key` = 'inscriptions_gestion.dossard_print';

DELETE FROM info
WHERE info_prg = 'compte.php'
  AND info_clef IN (
    'inscr_gestion_btn_dossard_print',
    'inscr_gestion_btn_dossard_print_title'
  );

INSERT INTO info (info_clef, info_langue, info_texte, info_aide, info_prg, info_description, info_trie, info_actif, info_option1, info_option2, info_option3, info_creation) VALUES
('inscr_gestion_btn_dossard_print', 'fr', 'Imprimer le dossard', '', 'compte.php', 'MSIN-4485', 0, 1, '', '', '', NOW()),
('inscr_gestion_btn_dossard_print', 'en', 'Print bib', '', 'compte.php', 'MSIN-4485', 0, 1, '', '', '', NOW()),
('inscr_gestion_btn_dossard_print_title', 'fr', 'Générer le PDF du dossard', '', 'compte.php', 'MSIN-4485', 0, 1, '', '', '', NOW()),
('inscr_gestion_btn_dossard_print_title', 'en', 'Generate bib PDF', '', 'compte.php', 'MSIN-4485', 0, 1, '', '', '', NOW());
