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

package sqlc

import (
	"context"

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

const completeCompactionLog = `-- name: CompleteCompactionLog :one
UPDATE bot_history_message_compacts
SET status = $2,
    summary = $3,
    message_count = $4,
    error_message = $5,
    usage = $6,
    model_id = $7,
    completed_at = now()
WHERE id = $1
RETURNING id, bot_id, session_id, status, summary, message_count, error_message, usage, model_id, started_at, completed_at
`

type CompleteCompactionLogParams struct {
	ID           pgtype.UUID `json:"id"`
	Status       string      `json:"status"`
	Summary      string      `json:"summary"`
	MessageCount int32       `json:"message_count"`
	ErrorMessage string      `json:"error_message"`
	Usage        []byte      `json:"usage"`
	ModelID      pgtype.UUID `json:"model_id"`
}

func (q *Queries) CompleteCompactionLog(ctx context.Context, arg CompleteCompactionLogParams) (BotHistoryMessageCompact, error) {
	row := q.db.QueryRow(ctx, completeCompactionLog,
		arg.ID,
		arg.Status,
		arg.Summary,
		arg.MessageCount,
		arg.ErrorMessage,
		arg.Usage,
		arg.ModelID,
	)
	var i BotHistoryMessageCompact
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.SessionID,
		&i.Status,
		&i.Summary,
		&i.MessageCount,
		&i.ErrorMessage,
		&i.Usage,
		&i.ModelID,
		&i.StartedAt,
		&i.CompletedAt,
	)
	return i, err
}

const createCompactionLog = `-- name: CreateCompactionLog :one
INSERT INTO bot_history_message_compacts (bot_id, session_id)
VALUES ($1, $2)
RETURNING id, bot_id, session_id, status, summary, message_count, error_message, usage, model_id, started_at, completed_at
`

type CreateCompactionLogParams struct {
	BotID     pgtype.UUID `json:"bot_id"`
	SessionID pgtype.UUID `json:"session_id"`
}

func (q *Queries) CreateCompactionLog(ctx context.Context, arg CreateCompactionLogParams) (BotHistoryMessageCompact, error) {
	row := q.db.QueryRow(ctx, createCompactionLog, arg.BotID, arg.SessionID)
	var i BotHistoryMessageCompact
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.SessionID,
		&i.Status,
		&i.Summary,
		&i.MessageCount,
		&i.ErrorMessage,
		&i.Usage,
		&i.ModelID,
		&i.StartedAt,
		&i.CompletedAt,
	)
	return i, err
}

const deleteCompactionLogsByBot = `-- name: DeleteCompactionLogsByBot :exec
DELETE FROM bot_history_message_compacts WHERE bot_id = $1
`

func (q *Queries) DeleteCompactionLogsByBot(ctx context.Context, botID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteCompactionLogsByBot, botID)
	return err
}

const getCompactionLogByID = `-- name: GetCompactionLogByID :one
SELECT id, bot_id, session_id, status, summary, message_count, error_message, usage, model_id, started_at, completed_at
FROM bot_history_message_compacts
WHERE id = $1
`

func (q *Queries) GetCompactionLogByID(ctx context.Context, id pgtype.UUID) (BotHistoryMessageCompact, error) {
	row := q.db.QueryRow(ctx, getCompactionLogByID, id)
	var i BotHistoryMessageCompact
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.SessionID,
		&i.Status,
		&i.Summary,
		&i.MessageCount,
		&i.ErrorMessage,
		&i.Usage,
		&i.ModelID,
		&i.StartedAt,
		&i.CompletedAt,
	)
	return i, err
}

const listCompactionLogsByBot = `-- name: ListCompactionLogsByBot :many
SELECT id, bot_id, session_id, status, summary, message_count, error_message, usage, model_id, started_at, completed_at
FROM bot_history_message_compacts
WHERE bot_id = $1
  AND ($2::timestamptz IS NULL OR started_at < $2)
ORDER BY started_at DESC
LIMIT $3
`

type ListCompactionLogsByBotParams struct {
	BotID   pgtype.UUID        `json:"bot_id"`
	Column2 pgtype.Timestamptz `json:"column_2"`
	Limit   int32              `json:"limit"`
}

func (q *Queries) ListCompactionLogsByBot(ctx context.Context, arg ListCompactionLogsByBotParams) ([]BotHistoryMessageCompact, error) {
	rows, err := q.db.Query(ctx, listCompactionLogsByBot, arg.BotID, arg.Column2, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BotHistoryMessageCompact
	for rows.Next() {
		var i BotHistoryMessageCompact
		if err := rows.Scan(
			&i.ID,
			&i.BotID,
			&i.SessionID,
			&i.Status,
			&i.Summary,
			&i.MessageCount,
			&i.ErrorMessage,
			&i.Usage,
			&i.ModelID,
			&i.StartedAt,
			&i.CompletedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listCompactionLogsBySession = `-- name: ListCompactionLogsBySession :many
SELECT id, bot_id, session_id, status, summary, message_count, error_message, usage, model_id, started_at, completed_at
FROM bot_history_message_compacts
WHERE session_id = $1
ORDER BY started_at ASC
`

func (q *Queries) ListCompactionLogsBySession(ctx context.Context, sessionID pgtype.UUID) ([]BotHistoryMessageCompact, error) {
	rows, err := q.db.Query(ctx, listCompactionLogsBySession, sessionID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BotHistoryMessageCompact
	for rows.Next() {
		var i BotHistoryMessageCompact
		if err := rows.Scan(
			&i.ID,
			&i.BotID,
			&i.SessionID,
			&i.Status,
			&i.Summary,
			&i.MessageCount,
			&i.ErrorMessage,
			&i.Usage,
			&i.ModelID,
			&i.StartedAt,
			&i.CompletedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
