-- MSIN-4514 — Tri batch dossards : 4 niveaux (sort_3 / sort_4)
-- Prérequis : MSIN-4436-bib-sort-levels.sql (ba_bib_sort_1 / ba_bib_sort_2)
-- Notes : exécution UNIQUEMENT sur dev préprod ; autres env = Navicat structure + sync_static_db

SET @db := DATABASE();

-- Tri 3 (optionnel)
SET @col_sort3 := (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'inscriptions_epreuves' AND COLUMN_NAME = 'ba_bib_sort_3'
);
SET @sql_sort3 := IF(
    @col_sort3 = 0,
    'ALTER TABLE inscriptions_epreuves ADD COLUMN ba_bib_sort_3 VARCHAR(32) NOT NULL DEFAULT '''' COMMENT ''MSIN-4514 — tri batch niveau 3 (optionnel)'' AFTER ba_bib_sort_2',
    'SELECT ''ba_bib_sort_3 already exists'' AS note'
);
PREPARE stmt_sort3 FROM @sql_sort3;
EXECUTE stmt_sort3;
DEALLOCATE PREPARE stmt_sort3;

-- Tri 4 (optionnel)
SET @col_sort4 := (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'inscriptions_epreuves' AND COLUMN_NAME = 'ba_bib_sort_4'
);
SET @sql_sort4 := IF(
    @col_sort4 = 0,
    'ALTER TABLE inscriptions_epreuves ADD COLUMN ba_bib_sort_4 VARCHAR(32) NOT NULL DEFAULT '''' COMMENT ''MSIN-4514 — tri batch niveau 4 (optionnel)'' AFTER ba_bib_sort_3',
    'SELECT ''ba_bib_sort_4 already exists'' AS note'
);
PREPARE stmt_sort4 FROM @sql_sort4;
EXECUTE stmt_sort4;
DEALLOCATE PREPARE stmt_sort4;

-- Libellés Info FR/EN
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)
SELECT 'bib_v4_batch_order_3', 'fr', 'Tri niveau 3', 'Critère optionnel après le tri secondaire.', 'compte.php', '', 0, 1, '', '', '', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM info WHERE info_clef = 'bib_v4_batch_order_3' AND info_langue = 'fr' AND info_prg = 'compte.php');

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)
SELECT 'bib_v4_batch_order_3', 'en', 'Sort level 3', 'Optional criterion after the secondary sort.', 'compte.php', '', 0, 1, '', '', '', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM info WHERE info_clef = 'bib_v4_batch_order_3' AND info_langue = 'en' AND info_prg = 'compte.php');

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)
SELECT 'bib_v4_batch_order_4', 'fr', 'Tri niveau 4', 'Critère optionnel après le tri niveau 3.', 'compte.php', '', 0, 1, '', '', '', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM info WHERE info_clef = 'bib_v4_batch_order_4' AND info_langue = 'fr' AND info_prg = 'compte.php');

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)
SELECT 'bib_v4_batch_order_4', 'en', 'Sort level 4', 'Optional criterion after sort level 3.', 'compte.php', '', 0, 1, '', '', '', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM info WHERE info_clef = 'bib_v4_batch_order_4' AND info_langue = 'en' AND info_prg = 'compte.php');

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)
SELECT 'bib_v4_batch_sort_group_infos', 'fr', 'Infos participant', 'Champs infos participant activés pour l''événement (même source que Production Excel).', 'compte.php', '', 0, 1, '', '', '', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM info WHERE info_clef = 'bib_v4_batch_sort_group_infos' AND info_langue = 'fr' AND info_prg = 'compte.php');

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)
SELECT 'bib_v4_batch_sort_group_infos', 'en', 'Participant info', 'Event participant info fields (same source as Excel production).', 'compte.php', '', 0, 1, '', '', '', NOW()
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM info WHERE info_clef = 'bib_v4_batch_sort_group_infos' AND info_langue = 'en' AND info_prg = 'compte.php');
