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

package gen

import (
	"context"
	"database/sql"
	"time"

	"github.com/aoagents/agent-orchestrator/backend/internal/domain"
)

const getReviewBySession = `-- name: GetReviewBySession :one
SELECT id, session_id, project_id, harness, pr_url, reviewer_handle_id, created_at, updated_at
FROM review WHERE session_id = ?
`

func (q *Queries) GetReviewBySession(ctx context.Context, sessionID domain.SessionID) (Review, error) {
	row := q.db.QueryRowContext(ctx, getReviewBySession, sessionID)
	var i Review
	err := row.Scan(
		&i.ID,
		&i.SessionID,
		&i.ProjectID,
		&i.Harness,
		&i.PRURL,
		&i.ReviewerHandleID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getReviewRun = `-- name: GetReviewRun :one
SELECT id, review_id, session_id, harness, pr_url, target_sha, status, verdict, body, created_at, github_review_id, delivered_at, batch_id
FROM review_run WHERE id = ?
`

func (q *Queries) GetReviewRun(ctx context.Context, id string) (ReviewRun, error) {
	row := q.db.QueryRowContext(ctx, getReviewRun, id)
	var i ReviewRun
	err := row.Scan(
		&i.ID,
		&i.ReviewID,
		&i.SessionID,
		&i.Harness,
		&i.PRURL,
		&i.TargetSha,
		&i.Status,
		&i.Verdict,
		&i.Body,
		&i.CreatedAt,
		&i.GithubReviewID,
		&i.DeliveredAt,
		&i.BatchID,
	)
	return i, err
}

const getReviewRunBySessionPRAndSHA = `-- name: GetReviewRunBySessionPRAndSHA :one
SELECT id, review_id, session_id, harness, pr_url, target_sha, status, verdict, body, created_at, github_review_id, delivered_at, batch_id
FROM review_run WHERE session_id = ? AND pr_url = ? AND target_sha = ? ORDER BY created_at DESC LIMIT 1
`

type GetReviewRunBySessionPRAndSHAParams struct {
	SessionID domain.SessionID
	PRURL     string
	TargetSha string
}

func (q *Queries) GetReviewRunBySessionPRAndSHA(ctx context.Context, arg GetReviewRunBySessionPRAndSHAParams) (ReviewRun, error) {
	row := q.db.QueryRowContext(ctx, getReviewRunBySessionPRAndSHA, arg.SessionID, arg.PRURL, arg.TargetSha)
	var i ReviewRun
	err := row.Scan(
		&i.ID,
		&i.ReviewID,
		&i.SessionID,
		&i.Harness,
		&i.PRURL,
		&i.TargetSha,
		&i.Status,
		&i.Verdict,
		&i.Body,
		&i.CreatedAt,
		&i.GithubReviewID,
		&i.DeliveredAt,
		&i.BatchID,
	)
	return i, err
}

const insertReviewRun = `-- name: InsertReviewRun :exec
INSERT INTO review_run (id, review_id, session_id, batch_id, harness, pr_url, target_sha, status, verdict, body, github_review_id, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`

type InsertReviewRunParams struct {
	ID             string
	ReviewID       string
	SessionID      domain.SessionID
	BatchID        string
	Harness        domain.ReviewerHarness
	PRURL          string
	TargetSha      string
	Status         domain.ReviewRunStatus
	Verdict        domain.ReviewVerdict
	Body           string
	GithubReviewID string
	CreatedAt      time.Time
}

func (q *Queries) InsertReviewRun(ctx context.Context, arg InsertReviewRunParams) error {
	_, err := q.db.ExecContext(ctx, insertReviewRun,
		arg.ID,
		arg.ReviewID,
		arg.SessionID,
		arg.BatchID,
		arg.Harness,
		arg.PRURL,
		arg.TargetSha,
		arg.Status,
		arg.Verdict,
		arg.Body,
		arg.GithubReviewID,
		arg.CreatedAt,
	)
	return err
}

const listReviewRunsByBatch = `-- name: ListReviewRunsByBatch :many
SELECT id, review_id, session_id, harness, pr_url, target_sha, status, verdict, body, created_at, github_review_id, delivered_at, batch_id
FROM review_run WHERE session_id = ? AND batch_id = ? ORDER BY created_at ASC, id ASC
`

type ListReviewRunsByBatchParams struct {
	SessionID domain.SessionID
	BatchID   string
}

func (q *Queries) ListReviewRunsByBatch(ctx context.Context, arg ListReviewRunsByBatchParams) ([]ReviewRun, error) {
	rows, err := q.db.QueryContext(ctx, listReviewRunsByBatch, arg.SessionID, arg.BatchID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReviewRun{}
	for rows.Next() {
		var i ReviewRun
		if err := rows.Scan(
			&i.ID,
			&i.ReviewID,
			&i.SessionID,
			&i.Harness,
			&i.PRURL,
			&i.TargetSha,
			&i.Status,
			&i.Verdict,
			&i.Body,
			&i.CreatedAt,
			&i.GithubReviewID,
			&i.DeliveredAt,
			&i.BatchID,
		); 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 listReviewRunsBySession = `-- name: ListReviewRunsBySession :many
SELECT id, review_id, session_id, harness, pr_url, target_sha, status, verdict, body, created_at, github_review_id, delivered_at, batch_id
FROM review_run WHERE session_id = ? ORDER BY created_at DESC
`

func (q *Queries) ListReviewRunsBySession(ctx context.Context, sessionID domain.SessionID) ([]ReviewRun, error) {
	rows, err := q.db.QueryContext(ctx, listReviewRunsBySession, sessionID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReviewRun{}
	for rows.Next() {
		var i ReviewRun
		if err := rows.Scan(
			&i.ID,
			&i.ReviewID,
			&i.SessionID,
			&i.Harness,
			&i.PRURL,
			&i.TargetSha,
			&i.Status,
			&i.Verdict,
			&i.Body,
			&i.CreatedAt,
			&i.GithubReviewID,
			&i.DeliveredAt,
			&i.BatchID,
		); 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 markReviewRunDelivered = `-- name: MarkReviewRunDelivered :execrows
UPDATE review_run SET status = 'delivered', delivered_at = ? WHERE id = ? AND status = 'complete' AND delivered_at IS NULL
`

type MarkReviewRunDeliveredParams struct {
	DeliveredAt sql.NullTime
	ID          string
}

func (q *Queries) MarkReviewRunDelivered(ctx context.Context, arg MarkReviewRunDeliveredParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, markReviewRunDelivered, arg.DeliveredAt, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const supersedeReviewRun = `-- name: SupersedeReviewRun :execrows
UPDATE review_run SET status = 'failed', body = ? WHERE id = ? AND verdict = '' AND status != 'failed'
`

type SupersedeReviewRunParams struct {
	Body string
	ID   string
}

func (q *Queries) SupersedeReviewRun(ctx context.Context, arg SupersedeReviewRunParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, supersedeReviewRun, arg.Body, arg.ID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const supersedeStaleRunningReviewRuns = `-- name: SupersedeStaleRunningReviewRuns :execrows
UPDATE review_run SET status = 'failed', body = ? WHERE session_id = ? AND pr_url = ? AND target_sha != ? AND status = 'running' AND verdict = ''
`

type SupersedeStaleRunningReviewRunsParams struct {
	Body      string
	SessionID domain.SessionID
	PRURL     string
	TargetSha string
}

func (q *Queries) SupersedeStaleRunningReviewRuns(ctx context.Context, arg SupersedeStaleRunningReviewRunsParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, supersedeStaleRunningReviewRuns,
		arg.Body,
		arg.SessionID,
		arg.PRURL,
		arg.TargetSha,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const updateReviewRunResult = `-- name: UpdateReviewRunResult :execrows
UPDATE review_run SET status = ?, verdict = ?, body = ?, github_review_id = ? WHERE id = ? AND status = 'running'
`

type UpdateReviewRunResultParams struct {
	Status         domain.ReviewRunStatus
	Verdict        domain.ReviewVerdict
	Body           string
	GithubReviewID string
	ID             string
}

func (q *Queries) UpdateReviewRunResult(ctx context.Context, arg UpdateReviewRunResultParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, updateReviewRunResult,
		arg.Status,
		arg.Verdict,
		arg.Body,
		arg.GithubReviewID,
		arg.ID,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const upsertReview = `-- name: UpsertReview :exec
INSERT INTO review (id, session_id, project_id, harness, pr_url, reviewer_handle_id, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (session_id) DO UPDATE SET
    harness = excluded.harness,
    pr_url = excluded.pr_url,
    reviewer_handle_id = excluded.reviewer_handle_id,
    updated_at = excluded.updated_at
`

type UpsertReviewParams struct {
	ID               string
	SessionID        domain.SessionID
	ProjectID        domain.ProjectID
	Harness          domain.ReviewerHarness
	PRURL            string
	ReviewerHandleID string
	CreatedAt        time.Time
	UpdatedAt        time.Time
}

func (q *Queries) UpsertReview(ctx context.Context, arg UpsertReviewParams) error {
	_, err := q.db.ExecContext(ctx, upsertReview,
		arg.ID,
		arg.SessionID,
		arg.ProjectID,
		arg.Harness,
		arg.PRURL,
		arg.ReviewerHandleID,
		arg.CreatedAt,
		arg.UpdatedAt,
	)
	return err
}
