-- Tournament Scheduler Setup SQL
-- Execute this to set up the automatic tournament scheduling system
-- Note: This system uses a cron job to call tournament_cron_scheduler.cfm every minute

-- Add setting for normal turn speed (used when tournament becomes active)
INSERT INTO server_settings (setting_key, setting_value) 
VALUES ('server0_turnmin_normal', '0.05')
ON DUPLICATE KEY UPDATE setting_value = '0.05';

-- Add tournament status to handle registration phase
ALTER TABLE tournament_config 
MODIFY COLUMN status ENUM('scheduled', 'registration_open', 'active', 'complete', 'cancelled', 'suspended') DEFAULT 'scheduled';

-- Add indexes for better performance
CREATE INDEX IF NOT EXISTS idx_tournament_status_dates ON tournament_config(status, start_date, end_date);
CREATE INDEX IF NOT EXISTS idx_tournament_log_tournament_date ON tournament_log(tournament_id, action_date);
CREATE INDEX IF NOT EXISTS idx_event_attack_participants_date ON event_attack(source, target, datetime);

-- Sample scheduled tournament for testing (commented out)
-- Uncomment and modify dates to test the scheduler
/*
INSERT INTO tournament_config (
    tournament_name,
    tournament_password,
    status,
    players_per_team,
    server_id,
    start_date,
    end_date,
    description,
    rules,
    prize_info
) VALUES (
    'Test Scheduler Tournament',
    SHA2('testpass123', 256),
    'scheduled',
    5,
    0,
    DATE_ADD(NOW(), INTERVAL 5 MINUTE),   -- Starts in 5 minutes (for testing)
    DATE_ADD(NOW(), INTERVAL 2 HOUR),     -- Ends in 2 hours (for testing)
    'Test tournament to verify the automatic scheduler system.',
    'Standard rules. This tournament will automatically open registration and start.',
    'Testing rewards!'
);
*/