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

package gen

import (
	"context"
	"time"

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

const listChecksByPR = `-- name: ListChecksByPR :many
SELECT pr_url, name, commit_hash, status, url, log_tail, created_at, conclusion, details
FROM pr_checks WHERE pr_url = ? ORDER BY name, created_at
`

func (q *Queries) ListChecksByPR(ctx context.Context, prUrl string) ([]PRCheck, error) {
	rows, err := q.db.QueryContext(ctx, listChecksByPR, prUrl)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []PRCheck{}
	for rows.Next() {
		var i PRCheck
		if err := rows.Scan(
			&i.PRURL,
			&i.Name,
			&i.CommitHash,
			&i.Status,
			&i.URL,
			&i.LogTail,
			&i.CreatedAt,
			&i.Conclusion,
			&i.Details,
		); 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 upsertPRCheck = `-- name: UpsertPRCheck :exec
INSERT INTO pr_checks (pr_url, name, commit_hash, status, url, log_tail, created_at, conclusion, details)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (pr_url, name, commit_hash) DO UPDATE SET
    status = excluded.status,
    url = excluded.url,
    log_tail = excluded.log_tail,
    conclusion = excluded.conclusion,
    details = excluded.details
`

type UpsertPRCheckParams struct {
	PRURL      string
	Name       string
	CommitHash string
	Status     domain.PRCheckStatus
	URL        string
	LogTail    string
	CreatedAt  time.Time
	Conclusion string
	Details    string
}

func (q *Queries) UpsertPRCheck(ctx context.Context, arg UpsertPRCheckParams) error {
	_, err := q.db.ExecContext(ctx, upsertPRCheck,
		arg.PRURL,
		arg.Name,
		arg.CommitHash,
		arg.Status,
		arg.URL,
		arg.LogTail,
		arg.CreatedAt,
		arg.Conclusion,
		arg.Details,
	)
	return err
}
