-- v0.12.4: Performance & Scalabilite -- Critical indexes for query optimization
-- Reference: ORIGIN_PERFORMANCE_TARGETS.md S8.4
-- NOTE: All indexes use IF NOT EXISTS and DO $$ guards for missing tables

-- ============================================================================
-- USERS
-- ============================================================================
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
CREATE INDEX IF NOT EXISTS idx_users_created_at ON users(created_at DESC);

-- ============================================================================
-- TRACKS
-- ============================================================================
CREATE INDEX IF NOT EXISTS idx_tracks_creator_id ON tracks(creator_id);
CREATE INDEX IF NOT EXISTS idx_tracks_genre ON tracks(genre);
CREATE INDEX IF NOT EXISTS idx_tracks_created_at ON tracks(created_at DESC);

-- ============================================================================
-- MESSAGES
-- ============================================================================
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='messages') THEN
    CREATE INDEX IF NOT EXISTS idx_messages_room_created ON messages(room_id, created_at DESC);
    CREATE INDEX IF NOT EXISTS idx_messages_sender_id ON messages(sender_id);
  END IF;
END $$;

-- ============================================================================
-- PLAYLISTS
-- ============================================================================
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='playlists' AND column_name='creator_id') THEN
    CREATE INDEX IF NOT EXISTS idx_playlists_creator_id ON playlists(creator_id);
  END IF;
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='playlists' AND column_name='is_public') THEN
    EXECUTE 'CREATE INDEX IF NOT EXISTS idx_playlists_public_created ON playlists(is_public, created_at DESC)';
  END IF;
END $$;

-- ============================================================================
-- PLAYLIST TRACKS
-- ============================================================================
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='playlist_tracks') THEN
    CREATE INDEX IF NOT EXISTS idx_playlist_tracks_playlist ON playlist_tracks(playlist_id, position);
    CREATE INDEX IF NOT EXISTS idx_playlist_tracks_track ON playlist_tracks(track_id);
  END IF;
END $$;

-- ============================================================================
-- FOLLOWS
-- ============================================================================
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='follows') THEN
    CREATE INDEX IF NOT EXISTS idx_follows_follower ON follows(follower_id);
    CREATE INDEX IF NOT EXISTS idx_follows_following ON follows(followed_id);
  END IF;
END $$;

-- ============================================================================
-- COMMENTS
-- ============================================================================
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='comments') THEN
    CREATE INDEX IF NOT EXISTS idx_comments_user ON comments(user_id);
  END IF;
END $$;

-- ============================================================================
-- NOTIFICATIONS
-- ============================================================================
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='notifications') THEN
    CREATE INDEX IF NOT EXISTS idx_notifications_user_created ON notifications(user_id, created_at DESC);
  END IF;
END $$;

-- ============================================================================
-- ANALYTICS
-- ============================================================================
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='track_plays') THEN
    CREATE INDEX IF NOT EXISTS idx_track_plays_track_date ON track_plays(track_id, played_at DESC);
    CREATE INDEX IF NOT EXISTS idx_track_plays_user ON track_plays(user_id, played_at DESC);
  END IF;
END $$;

-- ============================================================================
-- MARKETPLACE (tables may not exist yet)
-- ============================================================================
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='marketplace_listings') THEN
    EXECUTE 'CREATE INDEX IF NOT EXISTS idx_marketplace_listings_status ON marketplace_listings(status, created_at DESC)';
  END IF;
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='marketplace_transactions') THEN
    CREATE INDEX IF NOT EXISTS idx_marketplace_transactions_buyer ON marketplace_transactions(buyer_id, created_at DESC);
    CREATE INDEX IF NOT EXISTS idx_marketplace_transactions_seller ON marketplace_transactions(seller_id, created_at DESC);
  END IF;
END $$;

-- ============================================================================
-- EDUCATION (tables may not exist yet)
-- ============================================================================
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='courses') THEN
    EXECUTE 'CREATE INDEX IF NOT EXISTS idx_courses_status_created ON courses(status, created_at DESC)';
  END IF;
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='course_enrollments') THEN
    CREATE INDEX IF NOT EXISTS idx_course_enrollments_user ON course_enrollments(user_id, created_at DESC);
  END IF;
END $$;

-- ============================================================================
-- FULL-TEXT SEARCH
-- ============================================================================
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='tracks' AND column_name='title') THEN
    CREATE INDEX IF NOT EXISTS idx_tracks_search_gin ON tracks
        USING GIN(to_tsvector('english', COALESCE(title, '') || ' ' || COALESCE(description, '')));
  END IF;
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='users' AND column_name='display_name') THEN
    CREATE INDEX IF NOT EXISTS idx_users_search_gin ON users
        USING GIN(to_tsvector('english', COALESCE(username, '') || ' ' || COALESCE(display_name, '')));
  END IF;
END $$;

-- ============================================================================
-- STATISTICS
-- ============================================================================
ANALYZE users;
ANALYZE tracks;
