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

package dbq

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const countArtists = `-- name: CountArtists :one
SELECT COUNT(*) FROM artists
`

func (q *Queries) CountArtists(ctx context.Context) (int64, error) {
	row := q.db.QueryRow(ctx, countArtists)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const countArtistsMatching = `-- name: CountArtistsMatching :one
SELECT COUNT(*) FROM artists WHERE name ILIKE '%' || $1::text || '%'
`

func (q *Queries) CountArtistsMatching(ctx context.Context, dollar_1 string) (int64, error) {
	row := q.db.QueryRow(ctx, countArtistsMatching, dollar_1)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const getArtistByID = `-- name: GetArtistByID :one
SELECT id, name, sort_name, mbid, created_at, updated_at FROM artists WHERE id = $1
`

func (q *Queries) GetArtistByID(ctx context.Context, id pgtype.UUID) (Artist, error) {
	row := q.db.QueryRow(ctx, getArtistByID, id)
	var i Artist
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.SortName,
		&i.Mbid,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getArtistByName = `-- name: GetArtistByName :one
SELECT id, name, sort_name, mbid, created_at, updated_at FROM artists WHERE name = $1 LIMIT 1
`

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

const listArtists = `-- name: ListArtists :many
SELECT id, name, sort_name, mbid, created_at, updated_at FROM artists ORDER BY sort_name
`

func (q *Queries) ListArtists(ctx context.Context) ([]Artist, error) {
	rows, err := q.db.Query(ctx, listArtists)
	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.SortName,
			&i.Mbid,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listArtistsAlpha = `-- name: ListArtistsAlpha :many
SELECT id, name, sort_name, mbid, created_at, updated_at FROM artists ORDER BY sort_name, name, id LIMIT $1 OFFSET $2
`

type ListArtistsAlphaParams struct {
	Limit  int32
	Offset int32
}

func (q *Queries) ListArtistsAlpha(ctx context.Context, arg ListArtistsAlphaParams) ([]Artist, error) {
	rows, err := q.db.Query(ctx, listArtistsAlpha, arg.Limit, arg.Offset)
	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.SortName,
			&i.Mbid,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listArtistsNewest = `-- name: ListArtistsNewest :many
SELECT id, name, sort_name, mbid, created_at, updated_at FROM artists ORDER BY created_at DESC, id DESC LIMIT $1 OFFSET $2
`

type ListArtistsNewestParams struct {
	Limit  int32
	Offset int32
}

// Secondary id tiebreaker keeps pagination stable when created_at ties.
func (q *Queries) ListArtistsNewest(ctx context.Context, arg ListArtistsNewestParams) ([]Artist, error) {
	rows, err := q.db.Query(ctx, listArtistsNewest, arg.Limit, arg.Offset)
	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.SortName,
			&i.Mbid,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const searchArtists = `-- name: SearchArtists :many
SELECT id, name, sort_name, mbid, created_at, updated_at FROM artists
WHERE name ILIKE '%' || $1 || '%'
ORDER BY sort_name
LIMIT $2 OFFSET $3
`

type SearchArtistsParams struct {
	Column1 *string
	Limit   int32
	Offset  int32
}

func (q *Queries) SearchArtists(ctx context.Context, arg SearchArtistsParams) ([]Artist, error) {
	rows, err := q.db.Query(ctx, searchArtists, arg.Column1, arg.Limit, arg.Offset)
	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.SortName,
			&i.Mbid,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const upsertArtist = `-- name: UpsertArtist :one
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 id, name, sort_name, mbid, created_at, updated_at
`

type UpsertArtistParams struct {
	Name     string
	SortName string
	Mbid     *string
}

// Insert or update by mbid when present; otherwise by (name, sort_name).
// Callers pass the canonical sort_name; scanner is responsible for derivation.
func (q *Queries) UpsertArtist(ctx context.Context, arg UpsertArtistParams) (Artist, error) {
	row := q.db.QueryRow(ctx, upsertArtist, arg.Name, arg.SortName, arg.Mbid)
	var i Artist
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.SortName,
		&i.Mbid,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
