-- MSIN-4328 — Dernier push ChronoTrack (détection changements via par_maj)
-- Prérequis : sql/MSIN-api-chronotrack-phase1-tables.sql
-- Idempotent. Exécuter avant déploiement PHP.

SET @db := DATABASE();

SET @col_lpa := (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'api_chronotrack_config' AND COLUMN_NAME = 'last_push_at'
);
SET @sql_lpa := IF(
    @col_lpa = 0,
    'ALTER TABLE api_chronotrack_config ADD COLUMN last_push_at DATETIME NULL DEFAULT NULL COMMENT ''MSIN-4328 — fin du dernier push OK'' AFTER updated_at',
    'SELECT ''last_push_at already exists'' AS note'
);
PREPARE stmt_lpa FROM @sql_lpa;
EXECUTE stmt_lpa;
DEALLOCATE PREPARE stmt_lpa;

SET @col_lpoc := (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'api_chronotrack_config' AND COLUMN_NAME = 'last_push_ok_count'
);
SET @sql_lpoc := IF(
    @col_lpoc = 0,
    'ALTER TABLE api_chronotrack_config ADD COLUMN last_push_ok_count INT UNSIGNED NOT NULL DEFAULT 0 COMMENT ''MSIN-4328 — nb OK dernier push'' AFTER last_push_at',
    'SELECT ''last_push_ok_count already exists'' AS note'
);
PREPARE stmt_lpoc FROM @sql_lpoc;
EXECUTE stmt_lpoc;
DEALLOCATE PREPARE stmt_lpoc;
