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

package sqlc

import (
	"context"

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

const checkTVShowExists = `-- name: CheckTVShowExists :one
select id, name, tt_imdb, popularity, created_at, updated_at from "tv_show"
where tt_imdb = $1
`

func (q *Queries) CheckTVShowExists(ctx context.Context, ttImdb string) (TvShow, error) {
	row := q.db.QueryRow(ctx, checkTVShowExists, ttImdb)
	var i TvShow
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.TtImdb,
		&i.Popularity,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const createEpisodes = `-- name: CreateEpisodes :one
insert into "episodes" (tv_show_id, season, episode, released, name, plot, avg_rating, vote_count)
values ($1, $2, $3, $4, $5, $6, $7, $8)
returning id, tv_show_id, season, episode, released, name, plot, avg_rating, vote_count
`

type CreateEpisodesParams struct {
	TvShowID  int32       `json:"tv_show_id"`
	Season    int32       `json:"season"`
	Episode   int32       `json:"episode"`
	Released  pgtype.Date `json:"released"`
	Name      string      `json:"name"`
	Plot      string      `json:"plot"`
	AvgRating float32     `json:"avg_rating"`
	VoteCount int32       `json:"vote_count"`
}

func (q *Queries) CreateEpisodes(ctx context.Context, arg CreateEpisodesParams) (Episode, error) {
	row := q.db.QueryRow(ctx, createEpisodes,
		arg.TvShowID,
		arg.Season,
		arg.Episode,
		arg.Released,
		arg.Name,
		arg.Plot,
		arg.AvgRating,
		arg.VoteCount,
	)
	var i Episode
	err := row.Scan(
		&i.ID,
		&i.TvShowID,
		&i.Season,
		&i.Episode,
		&i.Released,
		&i.Name,
		&i.Plot,
		&i.AvgRating,
		&i.VoteCount,
	)
	return i, err
}

const createTVShow = `-- name: CreateTVShow :one
insert into "tv_show" (name, tt_imdb)
values ($1, $2)
returning id, name, tt_imdb, popularity, created_at, updated_at
`

type CreateTVShowParams struct {
	Name   string `json:"name"`
	TtImdb string `json:"tt_imdb"`
}

func (q *Queries) CreateTVShow(ctx context.Context, arg CreateTVShowParams) (TvShow, error) {
	row := q.db.QueryRow(ctx, createTVShow, arg.Name, arg.TtImdb)
	var i TvShow
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.TtImdb,
		&i.Popularity,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getEpisodes = `-- name: GetEpisodes :many
select id, tv_show_id, season, episode, released, name, plot, avg_rating, vote_count from "episodes"
where tv_show_id = $1
order by season, episode asc
`

func (q *Queries) GetEpisodes(ctx context.Context, tvShowID int32) ([]Episode, error) {
	rows, err := q.db.Query(ctx, getEpisodes, tvShowID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Episode{}
	for rows.Next() {
		var i Episode
		if err := rows.Scan(
			&i.ID,
			&i.TvShowID,
			&i.Season,
			&i.Episode,
			&i.Released,
			&i.Name,
			&i.Plot,
			&i.AvgRating,
			&i.VoteCount,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const increasePopularity = `-- name: IncreasePopularity :exec
update "tv_show" set popularity = popularity + 1
where tt_imdb = $1
`

func (q *Queries) IncreasePopularity(ctx context.Context, ttImdb string) error {
	_, err := q.db.Exec(ctx, increasePopularity, ttImdb)
	return err
}

const seasonAverageRating = `-- name: SeasonAverageRating :one
select avg(avg_rating) from "episodes"
where tv_show_id = $1 and season = $2
`

type SeasonAverageRatingParams struct {
	TvShowID int32 `json:"tv_show_id"`
	Season   int32 `json:"season"`
}

func (q *Queries) SeasonAverageRating(ctx context.Context, arg SeasonAverageRatingParams) (float64, error) {
	row := q.db.QueryRow(ctx, seasonAverageRating, arg.TvShowID, arg.Season)
	var avg float64
	err := row.Scan(&avg)
	return avg, err
}

const seasonMedianRating = `-- name: SeasonMedianRating :one
select percentile_cont(0.5) within group (order by avg_rating) from "episodes"
where tv_show_id = $1 and season = $2
`

type SeasonMedianRatingParams struct {
	TvShowID int32 `json:"tv_show_id"`
	Season   int32 `json:"season"`
}

func (q *Queries) SeasonMedianRating(ctx context.Context, arg SeasonMedianRatingParams) (float64, error) {
	row := q.db.QueryRow(ctx, seasonMedianRating, arg.TvShowID, arg.Season)
	var percentile_cont float64
	err := row.Scan(&percentile_cont)
	return percentile_cont, err
}

const tvShowAverageRating = `-- name: TvShowAverageRating :one
select avg(avg_rating) from "episodes"
where tv_show_id = $1
`

func (q *Queries) TvShowAverageRating(ctx context.Context, tvShowID int32) (float64, error) {
	row := q.db.QueryRow(ctx, tvShowAverageRating, tvShowID)
	var avg float64
	err := row.Scan(&avg)
	return avg, err
}

const tvShowMedianRating = `-- name: TvShowMedianRating :one
select percentile_cont(0.5) within group (order by avg_rating) from "episodes"
where tv_show_id = $1
`

func (q *Queries) TvShowMedianRating(ctx context.Context, tvShowID int32) (float64, error) {
	row := q.db.QueryRow(ctx, tvShowMedianRating, tvShowID)
	var percentile_cont float64
	err := row.Scan(&percentile_cont)
	return percentile_cont, err
}
