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

package gen

import (
	"context"
	"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
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,
	)
	return i, err
}

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

type GetReviewRunBySessionAndSHAParams struct {
	SessionID domain.SessionID
	TargetSha string
}

func (q *Queries) GetReviewRunBySessionAndSHA(ctx context.Context, arg GetReviewRunBySessionAndSHAParams) (ReviewRun, error) {
	row := q.db.QueryRowContext(ctx, getReviewRunBySessionAndSHA, arg.SessionID, 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,
	)
	return i, err
}

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

type InsertReviewRunParams struct {
	ID        string
	ReviewID  string
	SessionID domain.SessionID
	Harness   domain.ReviewerHarness
	PRURL     string
	TargetSha string
	Status    domain.ReviewRunStatus
	Verdict   domain.ReviewVerdict
	Body      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.Harness,
		arg.PRURL,
		arg.TargetSha,
		arg.Status,
		arg.Verdict,
		arg.Body,
		arg.CreatedAt,
	)
	return err
}

const listReviewRunsBySession = `-- name: ListReviewRunsBySession :many
SELECT id, review_id, session_id, harness, pr_url, target_sha, status, verdict, body, created_at
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,
		); 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 updateReviewRunResult = `-- name: UpdateReviewRunResult :execrows
UPDATE review_run SET status = ?, verdict = ?, body = ? WHERE id = ? AND status = 'running'
`

type UpdateReviewRunResultParams struct {
	Status  domain.ReviewRunStatus
	Verdict domain.ReviewVerdict
	Body    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.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
}
