-- Map Analytics — Add FK tracking for map effectiveness metrics
-- Phase 2.2 Feature: Track which maps drive player count and rotation effectiveness

-- Add map_id to server_stats to correlate player counts with specific maps
ALTER TABLE server_stats ADD COLUMN map_id UUID REFERENCES map_library(id) ON DELETE SET NULL;
CREATE INDEX idx_server_stats_map ON server_stats(map_id);

-- Migrate wipe_history from string to FK (preserve legacy data for backward compat)
ALTER TABLE wipe_history ADD COLUMN map_id UUID REFERENCES map_library(id) ON DELETE SET NULL;
ALTER TABLE wipe_history RENAME COLUMN map_used TO map_used_legacy;
CREATE INDEX idx_wipe_history_map ON wipe_history(map_id);

COMMENT ON COLUMN server_stats.map_id IS 'FK to map_library — tracks which map was active when stats were recorded';
COMMENT ON COLUMN wipe_history.map_id IS 'FK to map_library — tracks which map was used for this wipe';
COMMENT ON COLUMN wipe_history.map_used_legacy IS 'Legacy string-based map name (preserved from pre-005 data)';
