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

package dbq

import (
	"context"

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

const countAlbums = `-- name: CountAlbums :one
SELECT COUNT(*) FROM albums
`

// M6a: total album count for the /api/library/albums envelope.
func (q *Queries) CountAlbums(ctx context.Context) (int64, error) {
	row := q.db.QueryRow(ctx, countAlbums)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const countAlbumsByArtist = `-- name: CountAlbumsByArtist :one
SELECT COUNT(*) FROM albums WHERE artist_id = $1
`

// Used by request-progress reporting to count how many albums have been
// ingested for a matched artist while a request is still in flight.
func (q *Queries) CountAlbumsByArtist(ctx context.Context, artistID pgtype.UUID) (int64, error) {
	row := q.db.QueryRow(ctx, countAlbumsByArtist, artistID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

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 deleteAlbumIfEmpty = `-- name: DeleteAlbumIfEmpty :one
DELETE FROM albums a
WHERE a.id = $1
  AND NOT EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id)
RETURNING a.id, a.artist_id
`

type DeleteAlbumIfEmptyRow struct {
	ID       pgtype.UUID
	ArtistID pgtype.UUID
}

// M7 #372: deletes the album row only when it has no remaining tracks.
// Used after a track delete to tidy up orphaned albums. RETURNING gives
// us the artist_id so the service can chain into the artist-empty check.
// Returns no rows when the album still has tracks; the service treats
// pgx.ErrNoRows as "album not orphaned, skip cascade".
func (q *Queries) DeleteAlbumIfEmpty(ctx context.Context, id pgtype.UUID) (DeleteAlbumIfEmptyRow, error) {
	row := q.db.QueryRow(ctx, deleteAlbumIfEmpty, id)
	var i DeleteAlbumIfEmptyRow
	err := row.Scan(&i.ID, &i.ArtistID)
	return i, 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 listAlbumsAlphaWithArtist = `-- name: ListAlbumsAlphaWithArtist :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.name AS artist_name
FROM albums
JOIN artists ON artists.id = albums.artist_id
ORDER BY albums.sort_title, albums.id
LIMIT $1 OFFSET $2
`

type ListAlbumsAlphaWithArtistParams struct {
	Limit  int32
	Offset int32
}

type ListAlbumsAlphaWithArtistRow struct {
	Album      Album
	ArtistName string
}

// M6a: alpha-sorted album list joined with artist_name. Used by
// /api/library/albums for the wrapping-grid page. Stable id-tiebreak.
func (q *Queries) ListAlbumsAlphaWithArtist(ctx context.Context, arg ListAlbumsAlphaWithArtistParams) ([]ListAlbumsAlphaWithArtistRow, error) {
	rows, err := q.db.Query(ctx, listAlbumsAlphaWithArtist, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListAlbumsAlphaWithArtistRow
	for rows.Next() {
		var i ListAlbumsAlphaWithArtistRow
		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.ArtistName,
		); 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 listRecentlyAddedAlbumsWithArtist = `-- name: ListRecentlyAddedAlbumsWithArtist :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.name AS artist_name
FROM albums
JOIN artists ON artists.id = albums.artist_id
ORDER BY albums.created_at DESC, albums.id
LIMIT $1
`

type ListRecentlyAddedAlbumsWithArtistRow struct {
	Album      Album
	ArtistName string
}

// M6a: recently-added albums joined with artist_name + artist_id for the
// home-page section. created_at is the row-insert timestamp from the
// scanner — the closest proxy to "added to library". Stable secondary
// ordering by id keeps pagination tie-break deterministic.
func (q *Queries) ListRecentlyAddedAlbumsWithArtist(ctx context.Context, limit int32) ([]ListRecentlyAddedAlbumsWithArtistRow, error) {
	rows, err := q.db.Query(ctx, listRecentlyAddedAlbumsWithArtist, limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListRecentlyAddedAlbumsWithArtistRow
	for rows.Next() {
		var i ListRecentlyAddedAlbumsWithArtistRow
		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.ArtistName,
		); 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
}
