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

package sqlc

import (
	"context"

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

const completeHeartbeatLog = `-- name: CompleteHeartbeatLog :one
UPDATE bot_heartbeat_logs
SET status = $2,
    result_text = $3,
    error_message = $4,
    usage = $5,
    model_id = $6,
    completed_at = now()
WHERE id = $1
RETURNING id, bot_id, session_id, status, result_text, error_message, usage, model_id, started_at, completed_at
`

type CompleteHeartbeatLogParams struct {
	ID           pgtype.UUID `json:"id"`
	Status       string      `json:"status"`
	ResultText   string      `json:"result_text"`
	ErrorMessage string      `json:"error_message"`
	Usage        []byte      `json:"usage"`
	ModelID      pgtype.UUID `json:"model_id"`
}

func (q *Queries) CompleteHeartbeatLog(ctx context.Context, arg CompleteHeartbeatLogParams) (BotHeartbeatLog, error) {
	row := q.db.QueryRow(ctx, completeHeartbeatLog,
		arg.ID,
		arg.Status,
		arg.ResultText,
		arg.ErrorMessage,
		arg.Usage,
		arg.ModelID,
	)
	var i BotHeartbeatLog
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.SessionID,
		&i.Status,
		&i.ResultText,
		&i.ErrorMessage,
		&i.Usage,
		&i.ModelID,
		&i.StartedAt,
		&i.CompletedAt,
	)
	return i, err
}

const createHeartbeatLog = `-- name: CreateHeartbeatLog :one
INSERT INTO bot_heartbeat_logs (bot_id, session_id, started_at)
VALUES ($1, $2::uuid, now())
RETURNING id, bot_id, session_id, status, result_text, error_message, usage, started_at, completed_at
`

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

type CreateHeartbeatLogRow struct {
	ID           pgtype.UUID        `json:"id"`
	BotID        pgtype.UUID        `json:"bot_id"`
	SessionID    pgtype.UUID        `json:"session_id"`
	Status       string             `json:"status"`
	ResultText   string             `json:"result_text"`
	ErrorMessage string             `json:"error_message"`
	Usage        []byte             `json:"usage"`
	StartedAt    pgtype.Timestamptz `json:"started_at"`
	CompletedAt  pgtype.Timestamptz `json:"completed_at"`
}

func (q *Queries) CreateHeartbeatLog(ctx context.Context, arg CreateHeartbeatLogParams) (CreateHeartbeatLogRow, error) {
	row := q.db.QueryRow(ctx, createHeartbeatLog, arg.BotID, arg.SessionID)
	var i CreateHeartbeatLogRow
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.SessionID,
		&i.Status,
		&i.ResultText,
		&i.ErrorMessage,
		&i.Usage,
		&i.StartedAt,
		&i.CompletedAt,
	)
	return i, err
}

const deleteHeartbeatLogsByBot = `-- name: DeleteHeartbeatLogsByBot :exec
DELETE FROM bot_heartbeat_logs WHERE bot_id = $1
`

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

const listHeartbeatLogsByBot = `-- name: ListHeartbeatLogsByBot :many
SELECT id, bot_id, session_id, status, result_text, error_message, usage, started_at, completed_at
FROM bot_heartbeat_logs
WHERE bot_id = $1
  AND ($2::timestamptz IS NULL OR started_at < $2::timestamptz)
ORDER BY started_at DESC
LIMIT $3
`

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

type ListHeartbeatLogsByBotRow struct {
	ID           pgtype.UUID        `json:"id"`
	BotID        pgtype.UUID        `json:"bot_id"`
	SessionID    pgtype.UUID        `json:"session_id"`
	Status       string             `json:"status"`
	ResultText   string             `json:"result_text"`
	ErrorMessage string             `json:"error_message"`
	Usage        []byte             `json:"usage"`
	StartedAt    pgtype.Timestamptz `json:"started_at"`
	CompletedAt  pgtype.Timestamptz `json:"completed_at"`
}

func (q *Queries) ListHeartbeatLogsByBot(ctx context.Context, arg ListHeartbeatLogsByBotParams) ([]ListHeartbeatLogsByBotRow, error) {
	rows, err := q.db.Query(ctx, listHeartbeatLogsByBot, arg.BotID, arg.Column2, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListHeartbeatLogsByBotRow
	for rows.Next() {
		var i ListHeartbeatLogsByBotRow
		if err := rows.Scan(
			&i.ID,
			&i.BotID,
			&i.SessionID,
			&i.Status,
			&i.ResultText,
			&i.ErrorMessage,
			&i.Usage,
			&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
}
