// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.28.0
// source: bundles.sql

package dbq

import (
	"context"

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

const getBundleWithID = `-- name: GetBundleWithID :one
SELECT id, site_id, name, created_at, updated_at FROM bundles WHERE id = $1
`

func (q *Queries) GetBundleWithID(ctx context.Context, id int64) (Bundle, error) {
	row := q.db.QueryRow(ctx, getBundleWithID, id)
	var i Bundle
	err := row.Scan(
		&i.ID,
		&i.SiteID,
		&i.Name,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getSiteBundleInfo = `-- name: GetSiteBundleInfo :many
WITH page_counts AS (
    SELECT b.bundle_id, count(*) AS page_count FROM pages b WHERE b.site_id = $1 GROUP BY bundle_id
), index_pages AS (
    SELECT p.id AS index_page_id, p.bundle_id FROM pages p WHERE p.site_id = $1 AND p.role = 'index'
)
SELECT b.bundle_id, b.page_count, p.index_page_id FROM page_counts b LEFT OUTER JOIN index_pages p ON b.bundle_id = p.bundle_id
`

type GetSiteBundleInfoRow struct {
	BundleID    int64
	PageCount   int64
	IndexPageID pgtype.Int8
}

func (q *Queries) GetSiteBundleInfo(ctx context.Context, siteID int64) ([]GetSiteBundleInfoRow, error) {
	rows, err := q.db.Query(ctx, getSiteBundleInfo, siteID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetSiteBundleInfoRow
	for rows.Next() {
		var i GetSiteBundleInfoRow
		if err := rows.Scan(&i.BundleID, &i.PageCount, &i.IndexPageID); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const insertBundle = `-- name: InsertBundle :one
INSERT INTO bundles (
   site_id,
   name,
   created_at,
   updated_at
) VALUES ($1, $2, $3, $3) RETURNING id
`

type InsertBundleParams struct {
	SiteID    int64
	Name      string
	CreatedAt pgtype.Timestamp
}

func (q *Queries) InsertBundle(ctx context.Context, arg InsertBundleParams) (int64, error) {
	row := q.db.QueryRow(ctx, insertBundle, arg.SiteID, arg.Name, arg.CreatedAt)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const listBundles = `-- name: ListBundles :many
SELECT id, site_id, name, created_at, updated_at FROM bundles WHERE site_id = $1
`

func (q *Queries) ListBundles(ctx context.Context, siteID int64) ([]Bundle, error) {
	rows, err := q.db.Query(ctx, listBundles, siteID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Bundle
	for rows.Next() {
		var i Bundle
		if err := rows.Scan(
			&i.ID,
			&i.SiteID,
			&i.Name,
			&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
}