// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: messages.sql

package chatsqlc

import (
	"context"

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

const deleteMessage = `-- name: DeleteMessage :exec
DELETE FROM messages WHERE id = $1
`

func (q *Queries) DeleteMessage(ctx context.Context, id int64) error {
	_, err := q.db.Exec(ctx, deleteMessage, id)
	return err
}

const getFirstUserMessage = `-- name: GetFirstUserMessage :one
SELECT id, conversation_id, role, content, thinking, tool_calls, status, error_message, prompt_tokens, completion_tokens, thinking_tokens, created_at, updated_at FROM messages
WHERE conversation_id = $1 AND role = 'user'
ORDER BY id ASC LIMIT 1
`

func (q *Queries) GetFirstUserMessage(ctx context.Context, conversationID pgtype.UUID) (Message, error) {
	row := q.db.QueryRow(ctx, getFirstUserMessage, conversationID)
	var i Message
	err := row.Scan(
		&i.ID,
		&i.ConversationID,
		&i.Role,
		&i.Content,
		&i.Thinking,
		&i.ToolCalls,
		&i.Status,
		&i.ErrorMessage,
		&i.PromptTokens,
		&i.CompletionTokens,
		&i.ThinkingTokens,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getMessage = `-- name: GetMessage :one
SELECT id, conversation_id, role, content, thinking, tool_calls, status, error_message, prompt_tokens, completion_tokens, thinking_tokens, created_at, updated_at FROM messages WHERE id = $1
`

func (q *Queries) GetMessage(ctx context.Context, id int64) (Message, error) {
	row := q.db.QueryRow(ctx, getMessage, id)
	var i Message
	err := row.Scan(
		&i.ID,
		&i.ConversationID,
		&i.Role,
		&i.Content,
		&i.Thinking,
		&i.ToolCalls,
		&i.Status,
		&i.ErrorMessage,
		&i.PromptTokens,
		&i.CompletionTokens,
		&i.ThinkingTokens,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const insertMessage = `-- name: InsertMessage :one
INSERT INTO messages (conversation_id, role, content, status)
VALUES ($1, $2, $3, $4)
RETURNING id, conversation_id, role, content, thinking, tool_calls, status, error_message, prompt_tokens, completion_tokens, thinking_tokens, created_at, updated_at
`

type InsertMessageParams struct {
	ConversationID pgtype.UUID `json:"conversation_id"`
	Role           string      `json:"role"`
	Content        string      `json:"content"`
	Status         string      `json:"status"`
}

func (q *Queries) InsertMessage(ctx context.Context, arg InsertMessageParams) (Message, error) {
	row := q.db.QueryRow(ctx, insertMessage,
		arg.ConversationID,
		arg.Role,
		arg.Content,
		arg.Status,
	)
	var i Message
	err := row.Scan(
		&i.ID,
		&i.ConversationID,
		&i.Role,
		&i.Content,
		&i.Thinking,
		&i.ToolCalls,
		&i.Status,
		&i.ErrorMessage,
		&i.PromptTokens,
		&i.CompletionTokens,
		&i.ThinkingTokens,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listMessagesAscending = `-- name: ListMessagesAscending :many
SELECT id, conversation_id, role, content, thinking, tool_calls, status, error_message, prompt_tokens, completion_tokens, thinking_tokens, created_at, updated_at FROM messages WHERE conversation_id = $1 ORDER BY id ASC
`

func (q *Queries) ListMessagesAscending(ctx context.Context, conversationID pgtype.UUID) ([]Message, error) {
	rows, err := q.db.Query(ctx, listMessagesAscending, conversationID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Message
	for rows.Next() {
		var i Message
		if err := rows.Scan(
			&i.ID,
			&i.ConversationID,
			&i.Role,
			&i.Content,
			&i.Thinking,
			&i.ToolCalls,
			&i.Status,
			&i.ErrorMessage,
			&i.PromptTokens,
			&i.CompletionTokens,
			&i.ThinkingTokens,
			&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
}

const listMessagesByConversation = `-- name: ListMessagesByConversation :many
SELECT id, conversation_id, role, content, thinking, tool_calls, status, error_message, prompt_tokens, completion_tokens, thinking_tokens, created_at, updated_at FROM messages
WHERE conversation_id = $1
  AND ($2::bigint = 0 OR id < $2)
ORDER BY id DESC
LIMIT $3
`

type ListMessagesByConversationParams struct {
	ConversationID pgtype.UUID `json:"conversation_id"`
	Column2        int64       `json:"column_2"`
	Limit          int32       `json:"limit"`
}

func (q *Queries) ListMessagesByConversation(ctx context.Context, arg ListMessagesByConversationParams) ([]Message, error) {
	rows, err := q.db.Query(ctx, listMessagesByConversation, arg.ConversationID, arg.Column2, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Message
	for rows.Next() {
		var i Message
		if err := rows.Scan(
			&i.ID,
			&i.ConversationID,
			&i.Role,
			&i.Content,
			&i.Thinking,
			&i.ToolCalls,
			&i.Status,
			&i.ErrorMessage,
			&i.PromptTokens,
			&i.CompletionTokens,
			&i.ThinkingTokens,
			&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
}

const markPendingAsErrorOnStartup = `-- name: MarkPendingAsErrorOnStartup :exec
UPDATE messages
SET status = 'error', error_message = 'server restart', updated_at = NOW()
WHERE status = 'pending'
`

func (q *Queries) MarkPendingAsErrorOnStartup(ctx context.Context) error {
	_, err := q.db.Exec(ctx, markPendingAsErrorOnStartup)
	return err
}

const updateMessageCancelled = `-- name: UpdateMessageCancelled :exec
UPDATE messages
SET status = 'cancelled', updated_at = NOW()
WHERE id = $1
`

func (q *Queries) UpdateMessageCancelled(ctx context.Context, id int64) error {
	_, err := q.db.Exec(ctx, updateMessageCancelled, id)
	return err
}

const updateMessageError = `-- name: UpdateMessageError :exec
UPDATE messages
SET status = 'error', error_message = $2, updated_at = NOW()
WHERE id = $1
`

type UpdateMessageErrorParams struct {
	ID           int64   `json:"id"`
	ErrorMessage *string `json:"error_message"`
}

func (q *Queries) UpdateMessageError(ctx context.Context, arg UpdateMessageErrorParams) error {
	_, err := q.db.Exec(ctx, updateMessageError, arg.ID, arg.ErrorMessage)
	return err
}

const updateMessageOK = `-- name: UpdateMessageOK :exec
UPDATE messages
SET content = $2, thinking = $3,
    prompt_tokens = $4, completion_tokens = $5, thinking_tokens = $6,
    status = 'ok', updated_at = NOW()
WHERE id = $1
`

type UpdateMessageOKParams struct {
	ID               int64   `json:"id"`
	Content          string  `json:"content"`
	Thinking         *string `json:"thinking"`
	PromptTokens     *int32  `json:"prompt_tokens"`
	CompletionTokens *int32  `json:"completion_tokens"`
	ThinkingTokens   *int32  `json:"thinking_tokens"`
}

func (q *Queries) UpdateMessageOK(ctx context.Context, arg UpdateMessageOKParams) error {
	_, err := q.db.Exec(ctx, updateMessageOK,
		arg.ID,
		arg.Content,
		arg.Thinking,
		arg.PromptTokens,
		arg.CompletionTokens,
		arg.ThinkingTokens,
	)
	return err
}
