-- Migration 005: Nexus Cross-Platform Integration
-- Adds friend system, game sessions, lobbies, and enhanced Nexus features

-- Ensure nexus_integrations table exists before ALTER TABLE
CREATE TABLE IF NOT EXISTS nexus_integrations (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid()
);

-- Extend nexus_integrations table with session and overlay config
ALTER TABLE nexus_integrations
ADD COLUMN IF NOT EXISTS current_game_session_id UUID,
ADD COLUMN IF NOT EXISTS game_state JSONB,
ADD COLUMN IF NOT EXISTS auto_mute_enabled BOOLEAN DEFAULT true,
ADD COLUMN IF NOT EXISTS overlay_enabled BOOLEAN DEFAULT true,
ADD COLUMN IF NOT EXISTS overlay_position VARCHAR(20) DEFAULT 'top-right';

-- Friend requests table
CREATE TABLE IF NOT EXISTS friend_requests (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  from_user_id VARCHAR NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  to_user_id VARCHAR NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  status VARCHAR(20) DEFAULT 'pending', -- pending, accepted, rejected
  created_at TIMESTAMP DEFAULT NOW(),
  responded_at TIMESTAMP,
  UNIQUE(from_user_id, to_user_id)
);

CREATE INDEX IF NOT EXISTS idx_friend_requests_to ON friend_requests(to_user_id, status);
CREATE INDEX IF NOT EXISTS idx_friend_requests_from ON friend_requests(from_user_id, status);

-- Friendships table
CREATE TABLE IF NOT EXISTS friendships (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user1_id VARCHAR NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  user2_id VARCHAR NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  created_at TIMESTAMP DEFAULT NOW(),
  CHECK (user1_id < user2_id), -- Prevent duplicates
  UNIQUE(user1_id, user2_id)
);

CREATE INDEX IF NOT EXISTS idx_friendships_user1 ON friendships(user1_id);
CREATE INDEX IF NOT EXISTS idx_friendships_user2 ON friendships(user2_id);

-- Game sessions table
CREATE TABLE IF NOT EXISTS game_sessions (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  nexus_player_id VARCHAR(100) NOT NULL,
  game_id VARCHAR(100) NOT NULL, -- Nexus game identifier
  game_name VARCHAR(200),
  session_state VARCHAR(20) DEFAULT 'active', -- active, paused, ended
  started_at TIMESTAMP DEFAULT NOW(),
  ended_at TIMESTAMP,
  duration_seconds INTEGER,
  metadata JSONB -- {mapName, gameMode, score, etc.}
);

-- Ensure started_at column exists for index creation
ALTER TABLE game_sessions ADD COLUMN IF NOT EXISTS started_at TIMESTAMP;

-- Ensure session_state column exists for index creation
ALTER TABLE game_sessions ADD COLUMN IF NOT EXISTS session_state VARCHAR;

-- Ensure nexus_player_id column exists for index creation
ALTER TABLE game_sessions ADD COLUMN IF NOT EXISTS nexus_player_id VARCHAR;

CREATE INDEX IF NOT EXISTS idx_game_sessions_user ON game_sessions(user_id, started_at DESC);
CREATE INDEX IF NOT EXISTS idx_game_sessions_active ON game_sessions(user_id, session_state);
CREATE INDEX IF NOT EXISTS idx_game_sessions_nexus_player ON game_sessions(nexus_player_id);

-- Game lobbies table
CREATE TABLE IF NOT EXISTS game_lobbies (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  game_id VARCHAR(100) NOT NULL,
  lobby_code VARCHAR(50) UNIQUE,
  host_user_id VARCHAR NOT NULL REFERENCES users(id),
  conversation_id UUID REFERENCES conversations(id), -- Auto-created chat
  max_players INTEGER DEFAULT 8,
  is_public BOOLEAN DEFAULT false,
  status VARCHAR(20) DEFAULT 'open', -- open, full, in-progress, closed
  created_at TIMESTAMP DEFAULT NOW(),
  started_at TIMESTAMP,
  ended_at TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_game_lobbies_game ON game_lobbies(game_id, status);
CREATE INDEX IF NOT EXISTS idx_game_lobbies_host ON game_lobbies(host_user_id);
CREATE INDEX IF NOT EXISTS idx_game_lobbies_code ON game_lobbies(lobby_code);

-- Game lobby participants table
CREATE TABLE IF NOT EXISTS game_lobby_participants (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  lobby_id UUID NOT NULL REFERENCES game_lobbies(id) ON DELETE CASCADE,
  user_id VARCHAR NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  team_id VARCHAR(20), -- For team-based games
  ready BOOLEAN DEFAULT false,
  joined_at TIMESTAMP DEFAULT NOW(),
  left_at TIMESTAMP,
  UNIQUE(lobby_id, user_id)
);

CREATE INDEX IF NOT EXISTS idx_lobby_participants_lobby ON game_lobby_participants(lobby_id);
CREATE INDEX IF NOT EXISTS idx_lobby_participants_user ON game_lobby_participants(user_id);

-- Add foreign key constraint for current_game_session_id
ALTER TABLE nexus_integrations
ADD CONSTRAINT fk_current_game_session 
FOREIGN KEY (current_game_session_id) 
REFERENCES game_sessions(id) 
ON DELETE SET NULL;
