-- ============================================
-- PERFORMANCE INDEXES FOR EXISTING TABLES
-- ============================================

-- Game table indexes
CREATE INDEX IF NOT EXISTS idx_game_deleted ON game(deleted) WHERE deleted IS NULL;
CREATE INDEX IF NOT EXISTS idx_game_hash ON game(hash);
CREATE INDEX IF NOT EXISTS idx_game_path ON game(path);
CREATE INDEX IF NOT EXISTS idx_game_name ON game(game_name);

-- Song table indexes  
CREATE INDEX IF NOT EXISTS idx_song_hash ON song(hash);
CREATE INDEX IF NOT EXISTS idx_song_path ON song(path);
CREATE INDEX IF NOT EXISTS idx_song_game_id ON song(game_id);
CREATE INDEX IF NOT EXISTS idx_song_game_id_song_name ON song(game_id, song_name);

-- Song_list table indexes
CREATE INDEX IF NOT EXISTS idx_song_list_match_date ON song_list(match_date);
CREATE INDEX IF NOT EXISTS idx_song_list_match_id ON song_list(match_id);

-- ============================================
-- SESSIONS TABLE FOR TOKEN MANAGEMENT
-- ============================================

-- Create sessions table for tracking client tokens
CREATE TABLE sessions (
    token VARCHAR(64) PRIMARY KEY,
    ip_address VARCHAR(45) NOT NULL,
    user_agent TEXT NOT NULL,
    client_type VARCHAR(20) DEFAULT 'web',
    expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Indexes for fast lookup and cleanup
CREATE INDEX idx_sessions_expires ON sessions(expires_at);
CREATE INDEX idx_sessions_token ON sessions(token);
CREATE INDEX idx_sessions_ip ON sessions(ip_address);
CREATE INDEX idx_sessions_created ON sessions(created_at);
