-- name: ListUserHistory :many
-- M7 #365: track-level listening history. Returns one row per
-- play_events entry, newest first, joined with tracks/albums/artists
-- so the SPA can render a TrackRef without follow-up fetches. Filters
-- out skipped events and tracks the user has quarantined (per-user
-- soft-hide). $1 = user_id, $2 = limit, $3 = offset.
SELECT pe.id           AS event_id,
       pe.started_at,
       sqlc.embed(t),
       albums.title    AS album_title,
       artists.name    AS artist_name
  FROM play_events pe
  JOIN tracks  t       ON t.id       = pe.track_id
  JOIN albums          ON albums.id  = t.album_id
  JOIN artists         ON artists.id = t.artist_id
 WHERE pe.user_id = $1
   AND pe.was_skipped = false
   AND NOT EXISTS (
       SELECT 1 FROM lidarr_quarantine q
        WHERE q.user_id = $1 AND q.track_id = t.id
   )
 ORDER BY pe.started_at DESC
 LIMIT $2 OFFSET $3;
