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

package sqlite

import (
	"context"
	"database/sql"
)

const createCollectionItemVersion = `-- name: CreateCollectionItemVersion :exec

INSERT INTO collection_item_versions (item_id, collection_id, site_id, html_content, template_id, position, created_by)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
`

type CreateCollectionItemVersionParams struct {
	ItemID       string `json:"item_id"`
	CollectionID string `json:"collection_id"`
	SiteID       string `json:"site_id"`
	HtmlContent  string `json:"html_content"`
	TemplateID   int64  `json:"template_id"`
	Position     int64  `json:"position"`
	CreatedBy    string `json:"created_by"`
}

// Collection item versions table queries
func (q *Queries) CreateCollectionItemVersion(ctx context.Context, arg CreateCollectionItemVersionParams) error {
	_, err := q.db.ExecContext(ctx, createCollectionItemVersion,
		arg.ItemID,
		arg.CollectionID,
		arg.SiteID,
		arg.HtmlContent,
		arg.TemplateID,
		arg.Position,
		arg.CreatedBy,
	)
	return err
}

const deleteOldCollectionItemVersions = `-- name: DeleteOldCollectionItemVersions :exec
DELETE FROM collection_item_versions 
WHERE created_at < ?1 AND site_id = ?2
`

type DeleteOldCollectionItemVersionsParams struct {
	CreatedBefore int64  `json:"created_before"`
	SiteID        string `json:"site_id"`
}

func (q *Queries) DeleteOldCollectionItemVersions(ctx context.Context, arg DeleteOldCollectionItemVersionsParams) error {
	_, err := q.db.ExecContext(ctx, deleteOldCollectionItemVersions, arg.CreatedBefore, arg.SiteID)
	return err
}

const getAllCollectionItemVersionsForSite = `-- name: GetAllCollectionItemVersionsForSite :many
SELECT 
    civ.version_id, civ.item_id, civ.collection_id, civ.site_id, civ.html_content, civ.template_id, civ.position, civ.created_at, civ.created_by,
    ci.html_content as current_html_content, ci.position as current_position
FROM collection_item_versions civ
LEFT JOIN collection_items ci ON civ.item_id = ci.item_id AND civ.collection_id = ci.collection_id AND civ.site_id = ci.site_id
WHERE civ.site_id = ?1
ORDER BY civ.created_at DESC
LIMIT ?2
`

type GetAllCollectionItemVersionsForSiteParams struct {
	SiteID     string `json:"site_id"`
	LimitCount int64  `json:"limit_count"`
}

type GetAllCollectionItemVersionsForSiteRow struct {
	VersionID          int64          `json:"version_id"`
	ItemID             string         `json:"item_id"`
	CollectionID       string         `json:"collection_id"`
	SiteID             string         `json:"site_id"`
	HtmlContent        string         `json:"html_content"`
	TemplateID         int64          `json:"template_id"`
	Position           int64          `json:"position"`
	CreatedAt          int64          `json:"created_at"`
	CreatedBy          string         `json:"created_by"`
	CurrentHtmlContent sql.NullString `json:"current_html_content"`
	CurrentPosition    sql.NullInt64  `json:"current_position"`
}

func (q *Queries) GetAllCollectionItemVersionsForSite(ctx context.Context, arg GetAllCollectionItemVersionsForSiteParams) ([]GetAllCollectionItemVersionsForSiteRow, error) {
	rows, err := q.db.QueryContext(ctx, getAllCollectionItemVersionsForSite, arg.SiteID, arg.LimitCount)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetAllCollectionItemVersionsForSiteRow
	for rows.Next() {
		var i GetAllCollectionItemVersionsForSiteRow
		if err := rows.Scan(
			&i.VersionID,
			&i.ItemID,
			&i.CollectionID,
			&i.SiteID,
			&i.HtmlContent,
			&i.TemplateID,
			&i.Position,
			&i.CreatedAt,
			&i.CreatedBy,
			&i.CurrentHtmlContent,
			&i.CurrentPosition,
		); 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 getCollectionItemVersion = `-- name: GetCollectionItemVersion :one
SELECT version_id, item_id, collection_id, site_id, html_content, template_id, position, created_at, created_by
FROM collection_item_versions
WHERE version_id = ?1
`

func (q *Queries) GetCollectionItemVersion(ctx context.Context, versionID int64) (CollectionItemVersion, error) {
	row := q.db.QueryRowContext(ctx, getCollectionItemVersion, versionID)
	var i CollectionItemVersion
	err := row.Scan(
		&i.VersionID,
		&i.ItemID,
		&i.CollectionID,
		&i.SiteID,
		&i.HtmlContent,
		&i.TemplateID,
		&i.Position,
		&i.CreatedAt,
		&i.CreatedBy,
	)
	return i, err
}

const getCollectionItemVersionHistory = `-- name: GetCollectionItemVersionHistory :many
SELECT version_id, item_id, collection_id, site_id, html_content, template_id, position, created_at, created_by
FROM collection_item_versions
WHERE item_id = ?1 AND collection_id = ?2 AND site_id = ?3
ORDER BY created_at DESC
LIMIT ?4
`

type GetCollectionItemVersionHistoryParams struct {
	ItemID       string `json:"item_id"`
	CollectionID string `json:"collection_id"`
	SiteID       string `json:"site_id"`
	LimitCount   int64  `json:"limit_count"`
}

func (q *Queries) GetCollectionItemVersionHistory(ctx context.Context, arg GetCollectionItemVersionHistoryParams) ([]CollectionItemVersion, error) {
	rows, err := q.db.QueryContext(ctx, getCollectionItemVersionHistory,
		arg.ItemID,
		arg.CollectionID,
		arg.SiteID,
		arg.LimitCount,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []CollectionItemVersion
	for rows.Next() {
		var i CollectionItemVersion
		if err := rows.Scan(
			&i.VersionID,
			&i.ItemID,
			&i.CollectionID,
			&i.SiteID,
			&i.HtmlContent,
			&i.TemplateID,
			&i.Position,
			&i.CreatedAt,
			&i.CreatedBy,
		); 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
}
