-- ================================================================
-- Ensure at least ONE active election exists for demo
-- ================================================================

-- Check if election 1 exists and update it to be active (from init.sql)
UPDATE elections
SET
    is_active = TRUE,
    start_date = DATE_SUB(NOW(), INTERVAL 1 HOUR),
    end_date = DATE_ADD(NOW(), INTERVAL 7 DAY)
WHERE id = 1;

-- If no active elections exist, create one
INSERT IGNORE INTO elections (id, name, description, start_date, end_date, elgamal_p, elgamal_g, is_active, results_published)
SELECT
    1,
    'Election Présidentielle 2025',
    'Vote pour la présidence',
    DATE_SUB(NOW(), INTERVAL 1 HOUR),
    DATE_ADD(NOW(), INTERVAL 7 DAY),
    23,
    5,
    TRUE,
    FALSE
WHERE NOT EXISTS (SELECT 1 FROM elections WHERE id = 1);

-- Ensure election 1 has candidates (from init.sql)
INSERT IGNORE INTO candidates (id, election_id, name, description, `order`)
VALUES
    (1, 1, 'Alice Dupont', 'Candidate pour le changement', 1),
    (2, 1, 'Bob Martin', 'Candidate pour la stabilité', 2),
    (3, 1, 'Charlie Leclerc', 'Candidate pour l''innovation', 3),
    (4, 1, 'Diana Fontaine', 'Candidate pour l''environnement', 4);

-- Confirmation
SELECT 'Active elections configured' as status;
SELECT COUNT(*) as total_elections FROM elections;
SELECT COUNT(*) as active_elections FROM elections WHERE is_active = TRUE;
SELECT id, name, is_active, start_date, end_date FROM elections LIMIT 5;
