// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: collection_templates.sql

package postgresql

import (
	"context"
)

const createCollectionTemplate = `-- name: CreateCollectionTemplate :one
INSERT INTO collection_templates (collection_id, site_id, name, html_template, is_default)
VALUES ($1, $2, $3, $4, $5)
RETURNING template_id, collection_id, site_id, name, html_template, is_default, created_at
`

type CreateCollectionTemplateParams struct {
	CollectionID string `json:"collection_id"`
	SiteID       string `json:"site_id"`
	Name         string `json:"name"`
	HtmlTemplate string `json:"html_template"`
	IsDefault    bool   `json:"is_default"`
}

func (q *Queries) CreateCollectionTemplate(ctx context.Context, arg CreateCollectionTemplateParams) (CollectionTemplate, error) {
	row := q.db.QueryRowContext(ctx, createCollectionTemplate,
		arg.CollectionID,
		arg.SiteID,
		arg.Name,
		arg.HtmlTemplate,
		arg.IsDefault,
	)
	var i CollectionTemplate
	err := row.Scan(
		&i.TemplateID,
		&i.CollectionID,
		&i.SiteID,
		&i.Name,
		&i.HtmlTemplate,
		&i.IsDefault,
		&i.CreatedAt,
	)
	return i, err
}

const deleteCollectionTemplate = `-- name: DeleteCollectionTemplate :exec
DELETE FROM collection_templates 
WHERE template_id = $1
`

func (q *Queries) DeleteCollectionTemplate(ctx context.Context, templateID int32) error {
	_, err := q.db.ExecContext(ctx, deleteCollectionTemplate, templateID)
	return err
}

const deleteCollectionTemplates = `-- name: DeleteCollectionTemplates :exec
DELETE FROM collection_templates 
WHERE collection_id = $1 AND site_id = $2
`

type DeleteCollectionTemplatesParams struct {
	CollectionID string `json:"collection_id"`
	SiteID       string `json:"site_id"`
}

func (q *Queries) DeleteCollectionTemplates(ctx context.Context, arg DeleteCollectionTemplatesParams) error {
	_, err := q.db.ExecContext(ctx, deleteCollectionTemplates, arg.CollectionID, arg.SiteID)
	return err
}

const getCollectionTemplate = `-- name: GetCollectionTemplate :one

SELECT template_id, collection_id, site_id, name, html_template, is_default, created_at
FROM collection_templates 
WHERE template_id = $1
`

// Collection templates table queries
func (q *Queries) GetCollectionTemplate(ctx context.Context, templateID int32) (CollectionTemplate, error) {
	row := q.db.QueryRowContext(ctx, getCollectionTemplate, templateID)
	var i CollectionTemplate
	err := row.Scan(
		&i.TemplateID,
		&i.CollectionID,
		&i.SiteID,
		&i.Name,
		&i.HtmlTemplate,
		&i.IsDefault,
		&i.CreatedAt,
	)
	return i, err
}

const getCollectionTemplates = `-- name: GetCollectionTemplates :many
SELECT template_id, collection_id, site_id, name, html_template, is_default, created_at
FROM collection_templates 
WHERE collection_id = $1 AND site_id = $2
ORDER BY is_default DESC, created_at ASC
`

type GetCollectionTemplatesParams struct {
	CollectionID string `json:"collection_id"`
	SiteID       string `json:"site_id"`
}

func (q *Queries) GetCollectionTemplates(ctx context.Context, arg GetCollectionTemplatesParams) ([]CollectionTemplate, error) {
	rows, err := q.db.QueryContext(ctx, getCollectionTemplates, arg.CollectionID, arg.SiteID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []CollectionTemplate
	for rows.Next() {
		var i CollectionTemplate
		if err := rows.Scan(
			&i.TemplateID,
			&i.CollectionID,
			&i.SiteID,
			&i.Name,
			&i.HtmlTemplate,
			&i.IsDefault,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getDefaultTemplate = `-- name: GetDefaultTemplate :one
SELECT template_id, collection_id, site_id, name, html_template, is_default, created_at
FROM collection_templates 
WHERE collection_id = $1 AND site_id = $2 AND is_default = TRUE
LIMIT 1
`

type GetDefaultTemplateParams struct {
	CollectionID string `json:"collection_id"`
	SiteID       string `json:"site_id"`
}

func (q *Queries) GetDefaultTemplate(ctx context.Context, arg GetDefaultTemplateParams) (CollectionTemplate, error) {
	row := q.db.QueryRowContext(ctx, getDefaultTemplate, arg.CollectionID, arg.SiteID)
	var i CollectionTemplate
	err := row.Scan(
		&i.TemplateID,
		&i.CollectionID,
		&i.SiteID,
		&i.Name,
		&i.HtmlTemplate,
		&i.IsDefault,
		&i.CreatedAt,
	)
	return i, err
}

const setTemplateAsDefault = `-- name: SetTemplateAsDefault :exec
UPDATE collection_templates 
SET is_default = CASE 
    WHEN template_id = $1 THEN TRUE
    ELSE FALSE
END
WHERE collection_id = $2 AND site_id = $3
`

type SetTemplateAsDefaultParams struct {
	TemplateID   int32  `json:"template_id"`
	CollectionID string `json:"collection_id"`
	SiteID       string `json:"site_id"`
}

func (q *Queries) SetTemplateAsDefault(ctx context.Context, arg SetTemplateAsDefaultParams) error {
	_, err := q.db.ExecContext(ctx, setTemplateAsDefault, arg.TemplateID, arg.CollectionID, arg.SiteID)
	return err
}

const updateCollectionTemplate = `-- name: UpdateCollectionTemplate :one
UPDATE collection_templates 
SET name = $1, html_template = $2, is_default = $3
WHERE template_id = $4
RETURNING template_id, collection_id, site_id, name, html_template, is_default, created_at
`

type UpdateCollectionTemplateParams struct {
	Name         string `json:"name"`
	HtmlTemplate string `json:"html_template"`
	IsDefault    bool   `json:"is_default"`
	TemplateID   int32  `json:"template_id"`
}

func (q *Queries) UpdateCollectionTemplate(ctx context.Context, arg UpdateCollectionTemplateParams) (CollectionTemplate, error) {
	row := q.db.QueryRowContext(ctx, updateCollectionTemplate,
		arg.Name,
		arg.HtmlTemplate,
		arg.IsDefault,
		arg.TemplateID,
	)
	var i CollectionTemplate
	err := row.Scan(
		&i.TemplateID,
		&i.CollectionID,
		&i.SiteID,
		&i.Name,
		&i.HtmlTemplate,
		&i.IsDefault,
		&i.CreatedAt,
	)
	return i, err
}
