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

package dbq

import (
	"context"

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

const countAlbumsMatching = `-- name: CountAlbumsMatching :one
SELECT COUNT(*) FROM albums WHERE title ILIKE '%' || $1::text || '%'
`

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

const getAlbumByArtistAndTitle = `-- name: GetAlbumByArtistAndTitle :one
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at FROM albums WHERE artist_id = $1 AND title = $2 LIMIT 1
`

type GetAlbumByArtistAndTitleParams struct {
	ArtistID pgtype.UUID
	Title    string
}

// Scanner uses this for the no-mbid dedupe path: resolve-or-create.
func (q *Queries) GetAlbumByArtistAndTitle(ctx context.Context, arg GetAlbumByArtistAndTitleParams) (Album, error) {
	row := q.db.QueryRow(ctx, getAlbumByArtistAndTitle, arg.ArtistID, arg.Title)
	var i Album
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.SortTitle,
		&i.ArtistID,
		&i.ReleaseDate,
		&i.Mbid,
		&i.CoverArtPath,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getAlbumByID = `-- name: GetAlbumByID :one
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at FROM albums WHERE id = $1
`

func (q *Queries) GetAlbumByID(ctx context.Context, id pgtype.UUID) (Album, error) {
	row := q.db.QueryRow(ctx, getAlbumByID, id)
	var i Album
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.SortTitle,
		&i.ArtistID,
		&i.ReleaseDate,
		&i.Mbid,
		&i.CoverArtPath,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listAlbumsAlphaByArtist = `-- name: ListAlbumsAlphaByArtist :many
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, artists.sort_name AS artist_sort_name
FROM albums
JOIN artists ON artists.id = albums.artist_id
ORDER BY artists.sort_name, albums.sort_title
LIMIT $1 OFFSET $2
`

type ListAlbumsAlphaByArtistParams struct {
	Limit  int32
	Offset int32
}

type ListAlbumsAlphaByArtistRow struct {
	Album          Album
	ArtistSortName string
}

// Sorted by the owning artist's sort_name. Needed by Subsonic's
// alphabeticalByArtist album list type.
func (q *Queries) ListAlbumsAlphaByArtist(ctx context.Context, arg ListAlbumsAlphaByArtistParams) ([]ListAlbumsAlphaByArtistRow, error) {
	rows, err := q.db.Query(ctx, listAlbumsAlphaByArtist, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListAlbumsAlphaByArtistRow
	for rows.Next() {
		var i ListAlbumsAlphaByArtistRow
		if err := rows.Scan(
			&i.Album.ID,
			&i.Album.Title,
			&i.Album.SortTitle,
			&i.Album.ArtistID,
			&i.Album.ReleaseDate,
			&i.Album.Mbid,
			&i.Album.CoverArtPath,
			&i.Album.CreatedAt,
			&i.Album.UpdatedAt,
			&i.ArtistSortName,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listAlbumsAlphaByName = `-- name: ListAlbumsAlphaByName :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at FROM albums ORDER BY sort_title LIMIT $1 OFFSET $2
`

type ListAlbumsAlphaByNameParams struct {
	Limit  int32
	Offset int32
}

func (q *Queries) ListAlbumsAlphaByName(ctx context.Context, arg ListAlbumsAlphaByNameParams) ([]Album, error) {
	rows, err := q.db.Query(ctx, listAlbumsAlphaByName, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Album
	for rows.Next() {
		var i Album
		if err := rows.Scan(
			&i.ID,
			&i.Title,
			&i.SortTitle,
			&i.ArtistID,
			&i.ReleaseDate,
			&i.Mbid,
			&i.CoverArtPath,
			&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 listAlbumsByArtist = `-- name: ListAlbumsByArtist :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at FROM albums WHERE artist_id = $1 ORDER BY release_date NULLS LAST, sort_title
`

func (q *Queries) ListAlbumsByArtist(ctx context.Context, artistID pgtype.UUID) ([]Album, error) {
	rows, err := q.db.Query(ctx, listAlbumsByArtist, artistID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Album
	for rows.Next() {
		var i Album
		if err := rows.Scan(
			&i.ID,
			&i.Title,
			&i.SortTitle,
			&i.ArtistID,
			&i.ReleaseDate,
			&i.Mbid,
			&i.CoverArtPath,
			&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 listAlbumsByGenre = `-- name: ListAlbumsByGenre :many
SELECT DISTINCT ON (albums.id) albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at
FROM albums
JOIN tracks ON tracks.album_id = albums.id
WHERE tracks.genre = $1
ORDER BY albums.id, albums.sort_title
LIMIT $2 OFFSET $3
`

type ListAlbumsByGenreParams struct {
	Genre  *string
	Limit  int32
	Offset int32
}

// Album "belongs to" a genre if any of its tracks carry that genre.
func (q *Queries) ListAlbumsByGenre(ctx context.Context, arg ListAlbumsByGenreParams) ([]Album, error) {
	rows, err := q.db.Query(ctx, listAlbumsByGenre, arg.Genre, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Album
	for rows.Next() {
		var i Album
		if err := rows.Scan(
			&i.ID,
			&i.Title,
			&i.SortTitle,
			&i.ArtistID,
			&i.ReleaseDate,
			&i.Mbid,
			&i.CoverArtPath,
			&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 listAlbumsNewest = `-- name: ListAlbumsNewest :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at FROM albums ORDER BY created_at DESC LIMIT $1 OFFSET $2
`

type ListAlbumsNewestParams struct {
	Limit  int32
	Offset int32
}

func (q *Queries) ListAlbumsNewest(ctx context.Context, arg ListAlbumsNewestParams) ([]Album, error) {
	rows, err := q.db.Query(ctx, listAlbumsNewest, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Album
	for rows.Next() {
		var i Album
		if err := rows.Scan(
			&i.ID,
			&i.Title,
			&i.SortTitle,
			&i.ArtistID,
			&i.ReleaseDate,
			&i.Mbid,
			&i.CoverArtPath,
			&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 listAlbumsRandom = `-- name: ListAlbumsRandom :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at FROM albums ORDER BY random() LIMIT $1
`

func (q *Queries) ListAlbumsRandom(ctx context.Context, limit int32) ([]Album, error) {
	rows, err := q.db.Query(ctx, listAlbumsRandom, limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Album
	for rows.Next() {
		var i Album
		if err := rows.Scan(
			&i.ID,
			&i.Title,
			&i.SortTitle,
			&i.ArtistID,
			&i.ReleaseDate,
			&i.Mbid,
			&i.CoverArtPath,
			&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 searchAlbums = `-- name: SearchAlbums :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at FROM albums
WHERE title ILIKE '%' || $1 || '%'
ORDER BY sort_title
LIMIT $2 OFFSET $3
`

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

func (q *Queries) SearchAlbums(ctx context.Context, arg SearchAlbumsParams) ([]Album, error) {
	rows, err := q.db.Query(ctx, searchAlbums, arg.Column1, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Album
	for rows.Next() {
		var i Album
		if err := rows.Scan(
			&i.ID,
			&i.Title,
			&i.SortTitle,
			&i.ArtistID,
			&i.ReleaseDate,
			&i.Mbid,
			&i.CoverArtPath,
			&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 upsertAlbum = `-- name: UpsertAlbum :one
INSERT INTO albums (title, sort_title, artist_id, release_date, mbid, cover_art_path)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (mbid) WHERE mbid IS NOT NULL
DO UPDATE SET
    title = EXCLUDED.title,
    sort_title = EXCLUDED.sort_title,
    artist_id = EXCLUDED.artist_id,
    release_date = EXCLUDED.release_date,
    cover_art_path = EXCLUDED.cover_art_path,
    updated_at = now()
RETURNING id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at
`

type UpsertAlbumParams struct {
	Title        string
	SortTitle    string
	ArtistID     pgtype.UUID
	ReleaseDate  pgtype.Date
	Mbid         *string
	CoverArtPath *string
}

func (q *Queries) UpsertAlbum(ctx context.Context, arg UpsertAlbumParams) (Album, error) {
	row := q.db.QueryRow(ctx, upsertAlbum,
		arg.Title,
		arg.SortTitle,
		arg.ArtistID,
		arg.ReleaseDate,
		arg.Mbid,
		arg.CoverArtPath,
	)
	var i Album
	err := row.Scan(
		&i.ID,
		&i.Title,
		&i.SortTitle,
		&i.ArtistID,
		&i.ReleaseDate,
		&i.Mbid,
		&i.CoverArtPath,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
