-- 0034_you_might_like.up.sql — "You might like" Home rows (#790).
--
-- Per-user ranked lists of IN-LIBRARY albums/artists the listener
-- doesn't actively spin but is predicted to enjoy, derived from the
-- same similarity + like-weighted candidate engine that powers For-You
-- (rolled up from track scores to album/artist). Built in the daily
-- 3am BuildSystemPlaylists pass and atomic-replaced, exactly like the
-- system playlists; read back by /api/home.
--
-- Two thin tables (id + rank) rather than denormalized snapshots: the
-- Home read path hydrates album/artist refs through the existing
-- albums/artists joins, so a later metadata edit is reflected without
-- a rebuild. CASCADE on user/album/artist deletes keeps them clean.

CREATE TABLE you_might_like_albums (
    user_id   uuid        NOT NULL REFERENCES users(id)  ON DELETE CASCADE,
    album_id  uuid        NOT NULL REFERENCES albums(id) ON DELETE CASCADE,
    rank      integer     NOT NULL,
    built_at  timestamptz NOT NULL DEFAULT now(),
    PRIMARY KEY (user_id, album_id)
);

-- Read path orders by rank within a user; the index serves it directly.
CREATE INDEX you_might_like_albums_user_rank_idx
    ON you_might_like_albums (user_id, rank);

CREATE TABLE you_might_like_artists (
    user_id    uuid        NOT NULL REFERENCES users(id)   ON DELETE CASCADE,
    artist_id  uuid        NOT NULL REFERENCES artists(id) ON DELETE CASCADE,
    rank       integer     NOT NULL,
    built_at   timestamptz NOT NULL DEFAULT now(),
    PRIMARY KEY (user_id, artist_id)
);

CREATE INDEX you_might_like_artists_user_rank_idx
    ON you_might_like_artists (user_id, rank);
