// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: artists.sql

package sqlcgen

import (
	"context"
	"database/sql"
)

const deleteAllArtists = `-- name: DeleteAllArtists :exec
DELETE FROM artists
`

func (q *Queries) DeleteAllArtists(ctx context.Context) error {
	_, err := q.db.ExecContext(ctx, deleteAllArtists)
	return err
}

const deleteArtist = `-- name: DeleteArtist :exec
DELETE FROM artists WHERE id = ?
`

func (q *Queries) DeleteArtist(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, deleteArtist, id)
	return err
}

const getAlbumArtists = `-- name: GetAlbumArtists :many
SELECT DISTINCT a.id, a.name, a.mbid
FROM artists a
JOIN albums al ON al.artist_id = a.id
WHERE EXISTS (
    SELECT 1 FROM audio_files af
    WHERE af.album_id = al.id
      AND af.library_id = COALESCE(NULLIF(CAST(?1 AS INTEGER), 0), af.library_id)
)
ORDER BY a.name
`

func (q *Queries) GetAlbumArtists(ctx context.Context, libraryID int64) ([]Artist, error) {
	rows, err := q.db.QueryContext(ctx, getAlbumArtists, libraryID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Artist
	for rows.Next() {
		var i Artist
		if err := rows.Scan(&i.ID, &i.Name, &i.Mbid); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getAllArtists = `-- name: GetAllArtists :many
SELECT id, name, mbid FROM artists ORDER BY name
`

func (q *Queries) GetAllArtists(ctx context.Context) ([]Artist, error) {
	rows, err := q.db.QueryContext(ctx, getAllArtists)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Artist
	for rows.Next() {
		var i Artist
		if err := rows.Scan(&i.ID, &i.Name, &i.Mbid); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getArtist = `-- name: GetArtist :one
SELECT id, name, mbid FROM artists WHERE id = ? LIMIT 1
`

func (q *Queries) GetArtist(ctx context.Context, id int64) (Artist, error) {
	row := q.db.QueryRowContext(ctx, getArtist, id)
	var i Artist
	err := row.Scan(&i.ID, &i.Name, &i.Mbid)
	return i, err
}

const getArtistByFilePath = `-- name: GetArtistByFilePath :one
SELECT COALESCE(a.name, '') AS artist_name, COALESCE(a.mbid, '') AS artist_mbid
FROM audio_files af
LEFT JOIN artists a ON a.id = af.artist_id
WHERE af.file_path = ?
LIMIT 1
`

type GetArtistByFilePathRow struct {
	ArtistName string
	ArtistMbid string
}

func (q *Queries) GetArtistByFilePath(ctx context.Context, filePath string) (GetArtistByFilePathRow, error) {
	row := q.db.QueryRowContext(ctx, getArtistByFilePath, filePath)
	var i GetArtistByFilePathRow
	err := row.Scan(&i.ArtistName, &i.ArtistMbid)
	return i, err
}

const getArtistByName = `-- name: GetArtistByName :one
SELECT id, name, mbid FROM artists WHERE name = ? LIMIT 1
`

func (q *Queries) GetArtistByName(ctx context.Context, name string) (Artist, error) {
	row := q.db.QueryRowContext(ctx, getArtistByName, name)
	var i Artist
	err := row.Scan(&i.ID, &i.Name, &i.Mbid)
	return i, err
}

const getUnreferencedArtistIDs = `-- name: GetUnreferencedArtistIDs :many
SELECT id FROM artists a
WHERE NOT EXISTS (SELECT 1 FROM audio_files af WHERE af.artist_id = a.id)
  AND NOT EXISTS (SELECT 1 FROM albums al WHERE al.artist_id = a.id)
`

// Artists no file and no album points at any more.
func (q *Queries) GetUnreferencedArtistIDs(ctx context.Context) ([]int64, error) {
	rows, err := q.db.QueryContext(ctx, getUnreferencedArtistIDs)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []int64
	for rows.Next() {
		var id int64
		if err := rows.Scan(&id); err != nil {
			return nil, err
		}
		items = append(items, id)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const setArtistMBID = `-- name: SetArtistMBID :exec
UPDATE artists SET mbid = ? WHERE id = ?
`

type SetArtistMBIDParams struct {
	Mbid sql.NullString
	ID   int64
}

func (q *Queries) SetArtistMBID(ctx context.Context, arg SetArtistMBIDParams) error {
	_, err := q.db.ExecContext(ctx, setArtistMBID, arg.Mbid, arg.ID)
	return err
}

const upsertArtist = `-- name: UpsertArtist :one

INSERT INTO artists (name, mbid) VALUES (?, ?)
ON CONFLICT(name) DO UPDATE SET
    mbid = COALESCE(excluded.mbid, artists.mbid)
RETURNING id, name, mbid
`

type UpsertArtistParams struct {
	Name string
	Mbid sql.NullString
}

// Queries over artists.
//
// An artist row is reachable two ways: as a file's primary artist
// (audio_files.artist_id) and as an album's artist (albums.artist_id).
// Both used to route through artist_credit + artist_credit_artist,
// which is how "which album artists are in library 2" came to be a
// five-join subquery inside a three-join query.
func (q *Queries) UpsertArtist(ctx context.Context, arg UpsertArtistParams) (Artist, error) {
	row := q.db.QueryRowContext(ctx, upsertArtist, arg.Name, arg.Mbid)
	var i Artist
	err := row.Scan(&i.ID, &i.Name, &i.Mbid)
	return i, err
}
