-- depends_on: room
CREATE TABLE IF NOT EXISTS user_room_state (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    "user" UUID NOT NULL,
    room UUID NOT NULL REFERENCES room(id),
    last_read_seq BIGINT NOT NULL DEFAULT 0,
    last_read_at TIMESTAMPTZ,
    is_pinned BOOLEAN NOT NULL DEFAULT FALSE,
    is_muted BOOLEAN NOT NULL DEFAULT FALSE,
    hide_muted BOOLEAN NOT NULL DEFAULT FALSE,
    notify_level TEXT NOT NULL DEFAULT 'all',
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE ("user", room)
);

CREATE INDEX IF NOT EXISTS idx_user_room_state_user ON user_room_state ("user");
CREATE INDEX IF NOT EXISTS idx_user_room_state_room ON user_room_state (room);
CREATE INDEX IF NOT EXISTS idx_user_room_state_pinned ON user_room_state ("user", is_pinned) WHERE is_pinned = TRUE;
