-- name: UpsertArtist :one
-- Insert or update by mbid when present; otherwise by (name, sort_name).
-- Callers pass the canonical sort_name; scanner is responsible for derivation.
INSERT INTO artists (name, sort_name, mbid)
VALUES ($1, $2, $3)
ON CONFLICT (mbid) WHERE mbid IS NOT NULL
DO UPDATE SET
    name = EXCLUDED.name,
    sort_name = EXCLUDED.sort_name,
    updated_at = now()
RETURNING *;

-- name: GetArtistByID :one
SELECT * FROM artists WHERE id = $1;

-- name: GetArtistByName :one
SELECT * FROM artists WHERE name = $1 LIMIT 1;

-- name: ListArtists :many
SELECT * FROM artists ORDER BY sort_name;
