
-- L3 Schema: Player Features Data Mart
-- Based on FeatureRDD.md
-- Granularity: One row per player (Aggregated Profile)
-- Note: Some features requiring complex Demo parsing (Phase 5) are omitted or reserved.

CREATE TABLE IF NOT EXISTS dm_player_features (
    steam_id_64 TEXT PRIMARY KEY,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    total_matches INTEGER DEFAULT 0,

    -- ==========================================
    -- 0. Basic Features (Avg per match)
    -- ==========================================
    basic_avg_rating REAL,
    basic_avg_kd REAL,
    basic_avg_adr REAL,
    basic_avg_kast REAL,
    basic_avg_rws REAL,
    basic_avg_headshot_kills REAL,
    basic_headshot_rate REAL, -- Headshot kills / Total kills
    basic_avg_first_kill REAL,
    basic_avg_first_death REAL,
    basic_first_kill_rate REAL, -- FK / (FK + FD) or FK / Opening Duels
    basic_first_death_rate REAL,
    basic_avg_kill_2 REAL,
    basic_avg_kill_3 REAL,
    basic_avg_kill_4 REAL,
    basic_avg_kill_5 REAL,
    basic_avg_assisted_kill REAL,
    basic_avg_perfect_kill REAL,
    basic_avg_revenge_kill REAL,
    basic_avg_awp_kill REAL,
    basic_avg_jump_count REAL,
    basic_avg_mvps REAL,
    basic_avg_plants REAL,
    basic_avg_defuses REAL,
    basic_avg_flash_assists REAL,

    -- ==========================================
    -- 1. STA: Stability & Time Series
    -- ==========================================
    sta_last_30_rating REAL,
    sta_win_rating REAL,
    sta_loss_rating REAL,
    sta_rating_volatility REAL, -- StdDev of last 10 ratings
    sta_time_rating_corr REAL, -- Correlation between match duration/time and rating
    sta_fatigue_decay REAL, -- Perf drop in later matches of same day

    -- ==========================================
    -- 2. BAT: Battle / Duel Capabilities
    -- ==========================================
    bat_kd_diff_high_elo REAL,
    bat_kd_diff_low_elo REAL,
    -- bat_win_rate_vs_all REAL, -- Removed
    bat_avg_duel_win_rate REAL,
    bat_avg_duel_freq REAL,
    -- Distance based stats (Placeholder for Classic data)
    bat_win_rate_close REAL,
    bat_win_rate_mid REAL,
    bat_win_rate_far REAL,

    -- ==========================================
    -- 3. HPS: High Pressure Scenarios
    -- ==========================================
    hps_clutch_win_rate_1v1 REAL,
    hps_clutch_win_rate_1v2 REAL,
    hps_clutch_win_rate_1v3_plus REAL,
    hps_match_point_win_rate REAL,
    hps_undermanned_survival_time REAL,
    hps_pressure_entry_rate REAL, -- FK rate when team losing streak
    hps_momentum_multikill_rate REAL, -- Multi-kill rate when team winning streak
    hps_tilt_rating_drop REAL, -- Rating drop after getting knifed/BM'd
    hps_clutch_rating_rise REAL, -- Rating rise after clutch
    hps_comeback_kd_diff REAL,
    hps_losing_streak_kd_diff REAL,

    -- ==========================================
    -- 4. PTL: Pistol Round Specialist
    -- ==========================================
    ptl_pistol_kills REAL, -- Avg per pistol round? Or Total? Usually Avg per match or Rate
    ptl_pistol_multikills REAL,
    ptl_pistol_win_rate REAL, -- Personal win rate in pistol rounds
    ptl_pistol_kd REAL,
    ptl_pistol_util_efficiency REAL,

    -- ==========================================
    -- 5. T/CT: Side Preference
    -- ==========================================
    side_rating_ct REAL, -- Currently calculated as K/D
    side_rating_t REAL,
    side_kd_ct REAL, -- Explicit K/D
    side_kd_t REAL,
    side_win_rate_ct REAL, -- Round Win %
    side_win_rate_t REAL,
    side_first_kill_rate_ct REAL,
    side_first_kill_rate_t REAL,
    side_kd_diff_ct_t REAL, -- CT KD - T KD

    -- New Side Comparisons
    side_rating_diff_ct_t REAL,

    -- ==========================================
    -- 6. Party Size Performance
    -- ==========================================
    party_1_win_rate REAL,
    party_1_rating REAL,
    party_1_adr REAL,
    
    party_2_win_rate REAL,
    party_2_rating REAL,
    party_2_adr REAL,
    
    party_3_win_rate REAL,
    party_3_rating REAL,
    party_3_adr REAL,
    
    party_4_win_rate REAL,
    party_4_rating REAL,
    party_4_adr REAL,
    
    party_5_win_rate REAL,
    party_5_rating REAL,
    party_5_adr REAL,

    -- ==========================================
    -- 7. Rating Distribution (Performance Tiers)
    -- ==========================================
    rating_dist_carry_rate REAL,      -- > 1.5
    rating_dist_normal_rate REAL,     -- 1.0 - 1.5
    rating_dist_sacrifice_rate REAL,  -- 0.6 - 1.0
    rating_dist_sleeping_rate REAL,   -- < 0.6

    -- ==========================================
    -- 8. ELO Stratification (Performance vs ELO)
    -- ==========================================
    elo_lt1200_rating REAL,
    elo_1200_1400_rating REAL,
    elo_1400_1600_rating REAL,
    elo_1600_1800_rating REAL,
    elo_1800_2000_rating REAL,
    elo_gt2000_rating REAL,

    -- ==========================================
    -- 9. More Side Stats (Restored)
    -- ==========================================
    side_kast_ct REAL,
    side_kast_t REAL,
    side_rws_ct REAL,
    side_rws_t REAL,
    side_first_death_rate_ct REAL,
    side_first_death_rate_t REAL,
    side_multikill_rate_ct REAL,
    side_multikill_rate_t REAL,
    side_headshot_rate_ct REAL,
    side_headshot_rate_t REAL,
    side_defuses_ct REAL,
    side_plants_t REAL,
    side_planted_bomb_count INTEGER,
    side_defused_bomb_count INTEGER,

    -- ==========================================
    -- 6. UTIL: Utility Usage
    -- ==========================================
    util_avg_nade_dmg REAL,
    util_avg_flash_time REAL,
    util_avg_flash_enemy REAL,
    util_avg_flash_team REAL,
    util_usage_rate REAL,

    -- ==========================================  
    -- 7. Scores (0-100)
    -- ==========================================
    score_bat REAL,
    score_sta REAL,
    score_hps REAL,
    score_ptl REAL,
    score_tct REAL,
    score_util REAL
);

-- Optional: Detailed per-match feature table for time-series analysis
CREATE TABLE IF NOT EXISTS fact_match_features (
    match_id TEXT,
    steam_id_64 TEXT,
    
    -- Snapshots of the 6 dimensions for this specific match
    basic_rating REAL,
    sta_trend_pre_match REAL, -- Rating trend entering this match
    bat_duel_win_rate REAL,
    hps_clutch_success INTEGER,
    ptl_performance_score REAL,
    
    PRIMARY KEY (match_id, steam_id_64)
);
