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

package gen

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

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

const claimPRForSession = `-- name: ClaimPRForSession :exec
INSERT INTO pr (url, session_id, number, pr_state, review_decision, ci_state, mergeability, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (url) DO UPDATE SET
    session_id = excluded.session_id,
    review_decision = excluded.review_decision,
    updated_at = excluded.updated_at
`

type ClaimPRForSessionParams struct {
	URL            string
	SessionID      domain.SessionID
	Number         int64
	PRState        domain.PRState
	ReviewDecision domain.ReviewDecision
	CIState        domain.CIState
	Mergeability   domain.Mergeability
	UpdatedAt      time.Time
}

func (q *Queries) ClaimPRForSession(ctx context.Context, arg ClaimPRForSessionParams) error {
	_, err := q.db.ExecContext(ctx, claimPRForSession,
		arg.URL,
		arg.SessionID,
		arg.Number,
		arg.PRState,
		arg.ReviewDecision,
		arg.CIState,
		arg.Mergeability,
		arg.UpdatedAt,
	)
	return err
}

const getDisplayPRFactsBySession = `-- name: GetDisplayPRFactsBySession :one
SELECT
    pr.url,
    pr.number,
    pr.pr_state,
    pr.review_decision,
    pr.ci_state,
    pr.mergeability,
    pr.updated_at,
    EXISTS (
        SELECT 1
        FROM pr_comment
        WHERE pr_comment.pr_url = pr.url
          AND pr_comment.resolved = 0
          AND pr_comment.is_bot = 0
    ) AS review_comments
FROM pr
WHERE pr.session_id = ?
ORDER BY
    CASE WHEN pr.pr_state NOT IN ('merged', 'closed') THEN 0 ELSE 1 END,
    pr.updated_at DESC
LIMIT 1
`

type GetDisplayPRFactsBySessionRow struct {
	URL            string
	Number         int64
	PRState        domain.PRState
	ReviewDecision domain.ReviewDecision
	CIState        domain.CIState
	Mergeability   domain.Mergeability
	UpdatedAt      time.Time
	ReviewComments bool
}

func (q *Queries) GetDisplayPRFactsBySession(ctx context.Context, sessionID domain.SessionID) (GetDisplayPRFactsBySessionRow, error) {
	row := q.db.QueryRowContext(ctx, getDisplayPRFactsBySession, sessionID)
	var i GetDisplayPRFactsBySessionRow
	err := row.Scan(
		&i.URL,
		&i.Number,
		&i.PRState,
		&i.ReviewDecision,
		&i.CIState,
		&i.Mergeability,
		&i.UpdatedAt,
		&i.ReviewComments,
	)
	return i, err
}

const getPR = `-- name: GetPR :one
SELECT url, session_id, number, pr_state, review_decision, ci_state, mergeability, updated_at, provider, host, repo, source_branch, target_branch, head_sha, title, additions, deletions, changed_files, author, base_sha, merge_commit_sha, is_draft, is_merged, is_closed, provider_state, provider_mergeable, provider_merge_state_status, html_url, created_at_provider, updated_at_provider, merged_at_provider, closed_at_provider, metadata_hash, ci_hash, review_hash, observed_at, ci_observed_at, review_observed_at, last_nudge_signature FROM pr WHERE url = ?
`

func (q *Queries) GetPR(ctx context.Context, url string) (PR, error) {
	row := q.db.QueryRowContext(ctx, getPR, url)
	var i PR
	err := row.Scan(
		&i.URL,
		&i.SessionID,
		&i.Number,
		&i.PRState,
		&i.ReviewDecision,
		&i.CIState,
		&i.Mergeability,
		&i.UpdatedAt,
		&i.Provider,
		&i.Host,
		&i.Repo,
		&i.SourceBranch,
		&i.TargetBranch,
		&i.HeadSha,
		&i.Title,
		&i.Additions,
		&i.Deletions,
		&i.ChangedFiles,
		&i.Author,
		&i.BaseSha,
		&i.MergeCommitSha,
		&i.IsDraft,
		&i.IsMerged,
		&i.IsClosed,
		&i.ProviderState,
		&i.ProviderMergeable,
		&i.ProviderMergeStateStatus,
		&i.HtmlURL,
		&i.CreatedAtProvider,
		&i.UpdatedAtProvider,
		&i.MergedAtProvider,
		&i.ClosedAtProvider,
		&i.MetadataHash,
		&i.CIHash,
		&i.ReviewHash,
		&i.ObservedAt,
		&i.CIObservedAt,
		&i.ReviewObservedAt,
		&i.LastNudgeSignature,
	)
	return i, err
}

const getPRClaimAndOwner = `-- name: GetPRClaimAndOwner :one
SELECT pr.session_id, sessions.is_terminated
FROM pr
JOIN sessions ON sessions.id = pr.session_id
WHERE pr.url = ?
`

type GetPRClaimAndOwnerRow struct {
	SessionID    domain.SessionID
	IsTerminated bool
}

// Returns the current owner of a PR URL plus whether that owner is
// terminated. Used by the takeover guard inside the claim tx.
func (q *Queries) GetPRClaimAndOwner(ctx context.Context, url string) (GetPRClaimAndOwnerRow, error) {
	row := q.db.QueryRowContext(ctx, getPRClaimAndOwner, url)
	var i GetPRClaimAndOwnerRow
	err := row.Scan(&i.SessionID, &i.IsTerminated)
	return i, err
}

const getPRLastNudgeSignature = `-- name: GetPRLastNudgeSignature :one
SELECT last_nudge_signature FROM pr WHERE url = ?
`

func (q *Queries) GetPRLastNudgeSignature(ctx context.Context, url string) (string, error) {
	row := q.db.QueryRowContext(ctx, getPRLastNudgeSignature, url)
	var last_nudge_signature string
	err := row.Scan(&last_nudge_signature)
	return last_nudge_signature, err
}

const listPRsBySession = `-- name: ListPRsBySession :many
SELECT url, session_id, number, pr_state, review_decision, ci_state, mergeability, updated_at, provider, host, repo, source_branch, target_branch, head_sha, title, additions, deletions, changed_files, author, base_sha, merge_commit_sha, is_draft, is_merged, is_closed, provider_state, provider_mergeable, provider_merge_state_status, html_url, created_at_provider, updated_at_provider, merged_at_provider, closed_at_provider, metadata_hash, ci_hash, review_hash, observed_at, ci_observed_at, review_observed_at, last_nudge_signature FROM pr
WHERE session_id = ?
ORDER BY updated_at DESC
`

func (q *Queries) ListPRsBySession(ctx context.Context, sessionID domain.SessionID) ([]PR, error) {
	rows, err := q.db.QueryContext(ctx, listPRsBySession, sessionID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []PR{}
	for rows.Next() {
		var i PR
		if err := rows.Scan(
			&i.URL,
			&i.SessionID,
			&i.Number,
			&i.PRState,
			&i.ReviewDecision,
			&i.CIState,
			&i.Mergeability,
			&i.UpdatedAt,
			&i.Provider,
			&i.Host,
			&i.Repo,
			&i.SourceBranch,
			&i.TargetBranch,
			&i.HeadSha,
			&i.Title,
			&i.Additions,
			&i.Deletions,
			&i.ChangedFiles,
			&i.Author,
			&i.BaseSha,
			&i.MergeCommitSha,
			&i.IsDraft,
			&i.IsMerged,
			&i.IsClosed,
			&i.ProviderState,
			&i.ProviderMergeable,
			&i.ProviderMergeStateStatus,
			&i.HtmlURL,
			&i.CreatedAtProvider,
			&i.UpdatedAtProvider,
			&i.MergedAtProvider,
			&i.ClosedAtProvider,
			&i.MetadataHash,
			&i.CIHash,
			&i.ReviewHash,
			&i.ObservedAt,
			&i.CIObservedAt,
			&i.ReviewObservedAt,
			&i.LastNudgeSignature,
		); 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 updatePRLastNudgeSignature = `-- name: UpdatePRLastNudgeSignature :exec
UPDATE pr SET last_nudge_signature = ? WHERE url = ?
`

type UpdatePRLastNudgeSignatureParams struct {
	LastNudgeSignature string
	URL                string
}

func (q *Queries) UpdatePRLastNudgeSignature(ctx context.Context, arg UpdatePRLastNudgeSignatureParams) error {
	_, err := q.db.ExecContext(ctx, updatePRLastNudgeSignature, arg.LastNudgeSignature, arg.URL)
	return err
}

const upsertLegacyPR = `-- name: UpsertLegacyPR :exec
INSERT INTO pr (
    url, session_id, number, pr_state, review_decision, ci_state, mergeability, updated_at,
    is_draft, is_merged, is_closed
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (url) DO UPDATE SET
    number = excluded.number,
    pr_state = excluded.pr_state,
    review_decision = excluded.review_decision,
    ci_state = excluded.ci_state,
    mergeability = excluded.mergeability,
    updated_at = excluded.updated_at,
    is_draft = excluded.is_draft,
    is_merged = excluded.is_merged,
    is_closed = excluded.is_closed
`

type UpsertLegacyPRParams struct {
	URL            string
	SessionID      domain.SessionID
	Number         int64
	PRState        domain.PRState
	ReviewDecision domain.ReviewDecision
	CIState        domain.CIState
	Mergeability   domain.Mergeability
	UpdatedAt      time.Time
	IsDraft        int64
	IsMerged       int64
	IsClosed       int64
}

func (q *Queries) UpsertLegacyPR(ctx context.Context, arg UpsertLegacyPRParams) error {
	_, err := q.db.ExecContext(ctx, upsertLegacyPR,
		arg.URL,
		arg.SessionID,
		arg.Number,
		arg.PRState,
		arg.ReviewDecision,
		arg.CIState,
		arg.Mergeability,
		arg.UpdatedAt,
		arg.IsDraft,
		arg.IsMerged,
		arg.IsClosed,
	)
	return err
}

const upsertPR = `-- name: UpsertPR :exec
INSERT INTO pr (
    url, session_id, number, pr_state, review_decision, ci_state, mergeability, updated_at,
    provider, host, repo, source_branch, target_branch, head_sha, title,
    additions, deletions, changed_files, author, base_sha, merge_commit_sha,
    is_draft, is_merged, is_closed,
    provider_state, provider_mergeable, provider_merge_state_status, html_url,
    created_at_provider, updated_at_provider, merged_at_provider, closed_at_provider,
    metadata_hash, ci_hash, review_hash, observed_at, ci_observed_at, review_observed_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (url) DO UPDATE SET
    number = excluded.number,
    pr_state = excluded.pr_state,
    review_decision = excluded.review_decision,
    ci_state = excluded.ci_state,
    mergeability = excluded.mergeability,
    updated_at = excluded.updated_at,
    provider = excluded.provider,
    host = excluded.host,
    repo = excluded.repo,
    source_branch = excluded.source_branch,
    target_branch = excluded.target_branch,
    head_sha = excluded.head_sha,
    title = excluded.title,
    additions = excluded.additions,
    deletions = excluded.deletions,
    changed_files = excluded.changed_files,
    author = excluded.author,
    base_sha = excluded.base_sha,
    merge_commit_sha = excluded.merge_commit_sha,
    is_draft = excluded.is_draft,
    is_merged = excluded.is_merged,
    is_closed = excluded.is_closed,
    provider_state = excluded.provider_state,
    provider_mergeable = excluded.provider_mergeable,
    provider_merge_state_status = excluded.provider_merge_state_status,
    html_url = excluded.html_url,
    created_at_provider = excluded.created_at_provider,
    updated_at_provider = excluded.updated_at_provider,
    merged_at_provider = excluded.merged_at_provider,
    closed_at_provider = excluded.closed_at_provider,
    metadata_hash = excluded.metadata_hash,
    ci_hash = excluded.ci_hash,
    review_hash = excluded.review_hash,
    observed_at = excluded.observed_at,
    ci_observed_at = excluded.ci_observed_at,
    review_observed_at = excluded.review_observed_at
`

type UpsertPRParams struct {
	URL                      string
	SessionID                domain.SessionID
	Number                   int64
	PRState                  domain.PRState
	ReviewDecision           domain.ReviewDecision
	CIState                  domain.CIState
	Mergeability             domain.Mergeability
	UpdatedAt                time.Time
	Provider                 string
	Host                     string
	Repo                     string
	SourceBranch             string
	TargetBranch             string
	HeadSha                  string
	Title                    string
	Additions                int64
	Deletions                int64
	ChangedFiles             int64
	Author                   string
	BaseSha                  string
	MergeCommitSha           string
	IsDraft                  int64
	IsMerged                 int64
	IsClosed                 int64
	ProviderState            string
	ProviderMergeable        string
	ProviderMergeStateStatus string
	HtmlURL                  string
	CreatedAtProvider        sql.NullTime
	UpdatedAtProvider        sql.NullTime
	MergedAtProvider         sql.NullTime
	ClosedAtProvider         sql.NullTime
	MetadataHash             string
	CIHash                   string
	ReviewHash               string
	ObservedAt               sql.NullTime
	CIObservedAt             sql.NullTime
	ReviewObservedAt         sql.NullTime
}

func (q *Queries) UpsertPR(ctx context.Context, arg UpsertPRParams) error {
	_, err := q.db.ExecContext(ctx, upsertPR,
		arg.URL,
		arg.SessionID,
		arg.Number,
		arg.PRState,
		arg.ReviewDecision,
		arg.CIState,
		arg.Mergeability,
		arg.UpdatedAt,
		arg.Provider,
		arg.Host,
		arg.Repo,
		arg.SourceBranch,
		arg.TargetBranch,
		arg.HeadSha,
		arg.Title,
		arg.Additions,
		arg.Deletions,
		arg.ChangedFiles,
		arg.Author,
		arg.BaseSha,
		arg.MergeCommitSha,
		arg.IsDraft,
		arg.IsMerged,
		arg.IsClosed,
		arg.ProviderState,
		arg.ProviderMergeable,
		arg.ProviderMergeStateStatus,
		arg.HtmlURL,
		arg.CreatedAtProvider,
		arg.UpdatedAtProvider,
		arg.MergedAtProvider,
		arg.ClosedAtProvider,
		arg.MetadataHash,
		arg.CIHash,
		arg.ReviewHash,
		arg.ObservedAt,
		arg.CIObservedAt,
		arg.ReviewObservedAt,
	)
	return err
}
