-- 087_queue_sessions.sql
-- v0.203 Lot D1: Collaborative queue sessions

CREATE TABLE IF NOT EXISTS queue_sessions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    share_token VARCHAR(32) UNIQUE NOT NULL,
    creator_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS shared_queue_items (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    session_id UUID NOT NULL REFERENCES queue_sessions(id) ON DELETE CASCADE,
    track_id UUID NOT NULL REFERENCES tracks(id),
    position INT NOT NULL,
    added_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_queue_sessions_token ON queue_sessions(share_token);
CREATE INDEX IF NOT EXISTS idx_shared_queue_items_session ON shared_queue_items(session_id, position);
