-- Step 1: Create sport-specific team stats tables
-- Run separately to avoid memory issues

-- 1a. Create football_team_stats
CREATE TABLE IF NOT EXISTS football_team_stats (
    id SERIAL PRIMARY KEY,
    match_id TEXT NOT NULL,
    team_id TEXT NOT NULL,
    possession_percentage NUMERIC(5,2),
    shots_on_target INT,
    shots_off_target INT,
    total_shots INT,
    total_passes INT,
    corners INT,
    fouls INT,
    offsides INT,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    CONSTRAINT fk_football_team_stats_match FOREIGN KEY (match_id) REFERENCES matches(id) ON DELETE CASCADE,
    CONSTRAINT fk_football_team_stats_team FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE,
    CONSTRAINT uq_football_team_stats_match_team UNIQUE (match_id, team_id)
);

-- 1b. Create basketball_team_stats
CREATE TABLE IF NOT EXISTS basketball_team_stats (
    id SERIAL PRIMARY KEY,
    match_id TEXT NOT NULL,
    team_id TEXT NOT NULL,
    points INT,
    rebounds INT,
    assists INT,
    fg_made INT,
    fg_attempted INT,
    three_pt_made INT,
    three_pt_attempted INT,
    ft_made INT,
    ft_attempted INT,
    steals INT,
    blocks INT,
    turnovers INT,
    q1_score INT,
    q2_score INT,
    q3_score INT,
    q4_score INT,
    ot_score INT,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    CONSTRAINT fk_basketball_team_stats_match FOREIGN KEY (match_id) REFERENCES matches(id) ON DELETE CASCADE,
    CONSTRAINT fk_basketball_team_stats_team FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE,
    CONSTRAINT uq_basketball_team_stats_match_team UNIQUE (match_id, team_id)
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_football_team_stats_match ON football_team_stats(match_id);
CREATE INDEX IF NOT EXISTS idx_football_team_stats_team ON football_team_stats(team_id);
CREATE INDEX IF NOT EXISTS idx_basketball_team_stats_match ON basketball_team_stats(match_id);
CREATE INDEX IF NOT EXISTS idx_basketball_team_stats_team ON basketball_team_stats(team_id);
