-- ========================================
-- Game Mode Configuration for GCDM Matchmaking
-- ========================================
-- This file documents how to configure game modes for the dynamic matchmaking system
-- Each game mode needs specific settings in the server_settings table

USE `gcdm_global`;

-- ========================================
-- CORE GAME MODE SETTINGS
-- ========================================
-- Each game mode requires these basic settings:
-- 1. game_mode_<mode>_enabled: Whether the mode is available (1=yes, 0=no)
-- 2. game_mode_<mode>_display_name: The name shown to players
-- 3. match_team_size_<mode>: Number of players per team
-- 4. match_duration_minutes_<mode>: Match time limit
-- 5. queue_timeout_minutes_<mode>: Max time in queue before expiry (optional)
-- 6. mmr_k_factor_<mode>: MMR adjustment factor (optional, default 32)

-- ========================================
-- EXAMPLE: 5v5 RANKED MODE (DEFAULT)
-- ========================================
INSERT INTO `server_settings` (`setting_key`, `setting_value`, `description`, `created_at`, `updated_at`) VALUES
('game_mode_ranked_5v5_enabled', '1', 'Whether 5v5 ranked mode is available (1=yes, 0=no)', NOW(), NOW()),
('game_mode_ranked_5v5_display_name', '5v5 Ranked', 'Display name for 5v5 mode', NOW(), NOW()),
('match_team_size_ranked_5v5', '5', 'Number of players per team in 5v5 mode', NOW(), NOW()),
('match_duration_minutes_ranked_5v5', '60', 'Duration of 5v5 matches in minutes', NOW(), NOW()),
('queue_timeout_minutes_ranked_5v5', '30', 'Minutes before 5v5 queue entries expire', NOW(), NOW()),
('mmr_k_factor_ranked_5v5', '32', 'MMR adjustment factor for 5v5 (higher = more volatile)', NOW(), NOW())
ON DUPLICATE KEY UPDATE
    setting_value = VALUES(setting_value),
    updated_at = NOW();

-- ========================================
-- EXAMPLE: 2v2 RANKED MODE
-- ========================================
INSERT INTO `server_settings` (`setting_key`, `setting_value`, `description`, `created_at`, `updated_at`) VALUES
('game_mode_ranked_2v2_enabled', '1', 'Whether 2v2 ranked mode is available (1=yes, 0=no)', NOW(), NOW()),
('game_mode_ranked_2v2_display_name', '2v2 Ranked', 'Display name for 2v2 mode', NOW(), NOW()),
('match_team_size_ranked_2v2', '2', 'Number of players per team in 2v2 mode', NOW(), NOW()),
('match_duration_minutes_ranked_2v2', '30', 'Duration of 2v2 matches in minutes', NOW(), NOW()),
('queue_timeout_minutes_ranked_2v2', '20', 'Minutes before 2v2 queue entries expire', NOW(), NOW()),
('mmr_k_factor_ranked_2v2', '32', 'MMR adjustment factor for 2v2 (higher = more volatile)', NOW(), NOW())
ON DUPLICATE KEY UPDATE
    setting_value = VALUES(setting_value),
    updated_at = NOW();

-- ========================================
-- EXAMPLE: 1v1 DUEL MODE
-- ========================================
INSERT INTO `server_settings` (`setting_key`, `setting_value`, `description`, `created_at`, `updated_at`) VALUES
('game_mode_ranked_1v1_enabled', '0', 'Whether 1v1 ranked mode is available (1=yes, 0=no)', NOW(), NOW()),
('game_mode_ranked_1v1_display_name', '1v1 Duel', 'Display name for 1v1 mode', NOW(), NOW()),
('match_team_size_ranked_1v1', '1', 'Number of players per team in 1v1 mode', NOW(), NOW()),
('match_duration_minutes_ranked_1v1', '20', 'Duration of 1v1 matches in minutes', NOW(), NOW()),
('queue_timeout_minutes_ranked_1v1', '15', 'Minutes before 1v1 queue entries expire', NOW(), NOW()),
('mmr_k_factor_ranked_1v1', '40', 'MMR adjustment factor for 1v1 (higher = more volatile)', NOW(), NOW())
ON DUPLICATE KEY UPDATE
    setting_value = VALUES(setting_value),
    updated_at = NOW();

-- ========================================
-- EXAMPLE: 3v3 RANKED MODE
-- ========================================
INSERT INTO `server_settings` (`setting_key`, `setting_value`, `description`, `created_at`, `updated_at`) VALUES
('game_mode_ranked_3v3_enabled', '0', 'Whether 3v3 ranked mode is available (1=yes, 0=no)', NOW(), NOW()),
('game_mode_ranked_3v3_display_name', '3v3 Ranked', 'Display name for 3v3 mode', NOW(), NOW()),
('match_team_size_ranked_3v3', '3', 'Number of players per team in 3v3 mode', NOW(), NOW()),
('match_duration_minutes_ranked_3v3', '45', 'Duration of 3v3 matches in minutes', NOW(), NOW()),
('queue_timeout_minutes_ranked_3v3', '25', 'Minutes before 3v3 queue entries expire', NOW(), NOW()),
('mmr_k_factor_ranked_3v3', '32', 'MMR adjustment factor for 3v3 (higher = more volatile)', NOW(), NOW())
ON DUPLICATE KEY UPDATE
    setting_value = VALUES(setting_value),
    updated_at = NOW();

-- ========================================
-- QUERY TO VERIFY GAME MODE CONFIGURATION
-- ========================================
SELECT
    REPLACE(REPLACE(setting_key, 'game_mode_', ''), '_enabled', '') AS mode_key,
    MAX(CASE WHEN setting_key LIKE '%_enabled' THEN setting_value END) AS enabled,
    MAX(CASE WHEN setting_key LIKE '%_display_name' THEN setting_value END) AS display_name,
    MAX(CASE WHEN setting_key LIKE 'match_team_size_%' THEN setting_value END) AS team_size,
    MAX(CASE WHEN setting_key LIKE 'match_duration_minutes_%' THEN setting_value END) AS duration_minutes,
    MAX(CASE WHEN setting_key LIKE 'queue_timeout_minutes_%' THEN setting_value END) AS queue_timeout,
    MAX(CASE WHEN setting_key LIKE 'mmr_k_factor_%' THEN setting_value END) AS mmr_k_factor
FROM server_settings
WHERE setting_key LIKE 'game_mode_%_enabled'
   OR setting_key LIKE 'game_mode_%_display_name'
   OR setting_key LIKE 'match_team_size_%'
   OR setting_key LIKE 'match_duration_minutes_%'
   OR setting_key LIKE 'queue_timeout_minutes_%'
   OR setting_key LIKE 'mmr_k_factor_%'
GROUP BY REPLACE(REPLACE(setting_key, 'game_mode_', ''), '_enabled', '')
ORDER BY mode_key;

-- ========================================
-- FIX FOR LEGACY DATA
-- ========================================
-- If you have player_mmr records with incorrect game_mode keys (with _enabled suffix),
-- run this to fix them:

UPDATE player_mmr
SET game_mode = 'ranked_5v5'
WHERE game_mode = 'ranked_5v5_enabled';

UPDATE player_mmr
SET game_mode = 'ranked_2v2'
WHERE game_mode = 'ranked_2v2_enabled';

UPDATE player_mmr
SET game_mode = REPLACE(REPLACE(game_mode, 'game_mode_', ''), '_enabled', '')
WHERE game_mode LIKE '%_enabled';

-- ========================================
-- NOTES FOR ADMINISTRATORS
-- ========================================
-- 1. Mode keys should follow the pattern: ranked_<size> (e.g., ranked_1v1, ranked_2v2, ranked_5v5)
-- 2. To disable a mode, set game_mode_<mode>_enabled to '0'
-- 3. Team size determines minimum players needed: min_players = team_size * 2
-- 4. Players' MMR is tracked separately for each game mode in the player_mmr table
-- 5. The matchmaking scheduler automatically adapts to mode-specific team sizes
-- 6. Display names can be customized without affecting the mode key
-- 7. New modes are automatically detected by the UI without code changes