-- PostgreSQL Post-Migration Optimizations
-- Execute with: python -c "exec(open('run_optimization.py').read())"

-- 1. CRITICAL INDEXES (Most Important)
-- These are based on the most common query patterns and foreign key relationships

-- StratPlay table (largest table with complex queries)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_stratplay_game_id ON stratplay (game_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_stratplay_batter_id ON stratplay (batter_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_stratplay_pitcher_id ON stratplay (pitcher_id);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_stratplay_season_week ON stratplay (game_id, inning_num); -- For game order queries
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_stratplay_on_base_code ON stratplay (on_base_code); -- For situational hitting (bases loaded, etc.)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_stratplay_composite ON stratplay (batter_id, game_id) WHERE pa = 1; -- For batting stats

-- Player table (frequently joined)  
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_player_season ON player (season);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_player_team_season ON player (team_id, season);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_player_name ON player (name); -- For player searches
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_player_pos ON player (pos_1, season); -- For positional queries

-- BattingStat/PitchingStat tables (statistics queries)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_battingstat_player_season ON battingstat (player_id, season);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_battingstat_team_season ON battingstat (team_id, season);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_battingstat_week ON battingstat (season, week);

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_pitchingstat_player_season ON pitchingstat (player_id, season);  
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_pitchingstat_team_season ON pitchingstat (team_id, season);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_pitchingstat_week ON pitchingstat (season, week);

-- Team table
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_team_season ON team (season);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_team_abbrev_season ON team (abbrev, season);

-- StratGame table (game lookups)  
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_stratgame_season_week ON stratgame (season, week);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_stratgame_teams ON stratgame (away_team_id, home_team_id, season);

-- Decision table (pitcher decisions)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_decision_pitcher_season ON decision (pitcher_id, season);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_decision_game ON decision (game_id);

-- 2. PERFORMANCE OPTIMIZATIONS

-- Update table statistics (important after large data loads)
ANALYZE;

-- Set PostgreSQL-specific optimizations
-- Increase work_mem for complex queries (adjust based on available RAM)
-- SET work_mem = '256MB';  -- Uncomment if you have sufficient RAM

-- Enable parallel query execution for large aggregations  
-- SET max_parallel_workers_per_gather = 2;  -- Uncomment if you have multiple CPU cores

-- 3. MAINTENANCE OPTIMIZATIONS

-- Enable auto-vacuum for better ongoing performance
-- (Should already be enabled by default in PostgreSQL)

-- Create partial indexes for common filtered queries
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_stratplay_bases_loaded 
    ON stratplay (batter_id, game_id) 
    WHERE on_base_code = '111'; -- Bases loaded situations

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_player_active 
    ON player (id, season) 
    WHERE team_id IS NOT NULL; -- Active players only

-- 4. QUERY-SPECIFIC INDEXES

-- For season-based aggregation queries (common in stats APIs)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_battingstat_season_aggregate 
    ON battingstat (season, player_id, team_id);

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_pitchingstat_season_aggregate 
    ON pitchingstat (season, player_id, team_id);

-- For transaction/draft queries
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_transaction_season ON transaction (season);
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_draftpick_season ON draftpick (season);

COMMIT;