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

package acpsqlc

import (
	"context"

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

const getLatestTurnBySession = `-- name: GetLatestTurnBySession :one
SELECT id, session_id, turn_index, prompt_request_id, status,
       stop_reason, update_count, started_at, completed_at
FROM acp_turns
WHERE session_id = $1
ORDER BY turn_index DESC
LIMIT 1
`

func (q *Queries) GetLatestTurnBySession(ctx context.Context, sessionID pgtype.UUID) (AcpTurn, error) {
	row := q.db.QueryRow(ctx, getLatestTurnBySession, sessionID)
	var i AcpTurn
	err := row.Scan(
		&i.ID,
		&i.SessionID,
		&i.TurnIndex,
		&i.PromptRequestID,
		&i.Status,
		&i.StopReason,
		&i.UpdateCount,
		&i.StartedAt,
		&i.CompletedAt,
	)
	return i, err
}

const incrementTurnUpdateCount = `-- name: IncrementTurnUpdateCount :exec
UPDATE acp_turns
SET update_count = update_count + 1
WHERE session_id = $1 AND turn_index = $2 AND status = 'in_progress'
`

type IncrementTurnUpdateCountParams struct {
	SessionID pgtype.UUID `json:"session_id"`
	TurnIndex int32       `json:"turn_index"`
}

func (q *Queries) IncrementTurnUpdateCount(ctx context.Context, arg IncrementTurnUpdateCountParams) error {
	_, err := q.db.Exec(ctx, incrementTurnUpdateCount, arg.SessionID, arg.TurnIndex)
	return err
}

const insertTurn = `-- name: InsertTurn :one
INSERT INTO acp_turns (
  session_id, turn_index, prompt_request_id, status
) VALUES ($1, $2, $3, $4)
RETURNING id, session_id, turn_index, prompt_request_id, status,
          stop_reason, update_count, started_at, completed_at
`

type InsertTurnParams struct {
	SessionID       pgtype.UUID `json:"session_id"`
	TurnIndex       int32       `json:"turn_index"`
	PromptRequestID *string     `json:"prompt_request_id"`
	Status          string      `json:"status"`
}

func (q *Queries) InsertTurn(ctx context.Context, arg InsertTurnParams) (AcpTurn, error) {
	row := q.db.QueryRow(ctx, insertTurn,
		arg.SessionID,
		arg.TurnIndex,
		arg.PromptRequestID,
		arg.Status,
	)
	var i AcpTurn
	err := row.Scan(
		&i.ID,
		&i.SessionID,
		&i.TurnIndex,
		&i.PromptRequestID,
		&i.Status,
		&i.StopReason,
		&i.UpdateCount,
		&i.StartedAt,
		&i.CompletedAt,
	)
	return i, err
}

const listTurnsBySession = `-- name: ListTurnsBySession :many
SELECT id, session_id, turn_index, prompt_request_id, status,
       stop_reason, update_count, started_at, completed_at
FROM acp_turns
WHERE session_id = $1
ORDER BY turn_index ASC
`

func (q *Queries) ListTurnsBySession(ctx context.Context, sessionID pgtype.UUID) ([]AcpTurn, error) {
	rows, err := q.db.Query(ctx, listTurnsBySession, sessionID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []AcpTurn
	for rows.Next() {
		var i AcpTurn
		if err := rows.Scan(
			&i.ID,
			&i.SessionID,
			&i.TurnIndex,
			&i.PromptRequestID,
			&i.Status,
			&i.StopReason,
			&i.UpdateCount,
			&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 markTurnAborted = `-- name: MarkTurnAborted :exec
UPDATE acp_turns
SET status = 'aborted', completed_at = now()
WHERE session_id = $1 AND turn_index = $2 AND status = 'in_progress'
`

type MarkTurnAbortedParams struct {
	SessionID pgtype.UUID `json:"session_id"`
	TurnIndex int32       `json:"turn_index"`
}

func (q *Queries) MarkTurnAborted(ctx context.Context, arg MarkTurnAbortedParams) error {
	_, err := q.db.Exec(ctx, markTurnAborted, arg.SessionID, arg.TurnIndex)
	return err
}

const markTurnCompleted = `-- name: MarkTurnCompleted :exec
UPDATE acp_turns
SET status = 'completed', stop_reason = $3, update_count = $4, completed_at = now()
WHERE session_id = $1 AND turn_index = $2 AND status = 'in_progress'
`

type MarkTurnCompletedParams struct {
	SessionID   pgtype.UUID `json:"session_id"`
	TurnIndex   int32       `json:"turn_index"`
	StopReason  *string     `json:"stop_reason"`
	UpdateCount int32       `json:"update_count"`
}

func (q *Queries) MarkTurnCompleted(ctx context.Context, arg MarkTurnCompletedParams) error {
	_, err := q.db.Exec(ctx, markTurnCompleted,
		arg.SessionID,
		arg.TurnIndex,
		arg.StopReason,
		arg.UpdateCount,
	)
	return err
}

const purgeTurnsBefore = `-- name: PurgeTurnsBefore :execrows
DELETE FROM acp_turns WHERE started_at < $1
`

func (q *Queries) PurgeTurnsBefore(ctx context.Context, startedAt pgtype.Timestamptz) (int64, error) {
	result, err := q.db.Exec(ctx, purgeTurnsBefore, startedAt)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}
