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

package projectsqlc

import (
	"context"

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

const countActiveSessionsForProject = `-- name: CountActiveSessionsForProject :one
SELECT COUNT(*) FROM acp_sessions
WHERE project_id = $1 AND status IN ('starting', 'running')
`

// Active (starting/running) acp_sessions in a project. Used by the scheduler's
// soft per-project concurrency pre-check.
func (q *Queries) CountActiveSessionsForProject(ctx context.Context, projectID pgtype.UUID) (int64, error) {
	row := q.db.QueryRow(ctx, countActiveSessionsForProject, projectID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createDependency = `-- name: CreateDependency :one
INSERT INTO issue_dependencies (id, project_id, blocked_id, blocker_id, created_by)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, project_id, blocked_id, blocker_id, created_by, created_at
`

type CreateDependencyParams struct {
	ID        pgtype.UUID `json:"id"`
	ProjectID pgtype.UUID `json:"project_id"`
	BlockedID pgtype.UUID `json:"blocked_id"`
	BlockerID pgtype.UUID `json:"blocker_id"`
	CreatedBy pgtype.UUID `json:"created_by"`
}

func (q *Queries) CreateDependency(ctx context.Context, arg CreateDependencyParams) (IssueDependency, error) {
	row := q.db.QueryRow(ctx, createDependency,
		arg.ID,
		arg.ProjectID,
		arg.BlockedID,
		arg.BlockerID,
		arg.CreatedBy,
	)
	var i IssueDependency
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.BlockedID,
		&i.BlockerID,
		&i.CreatedBy,
		&i.CreatedAt,
	)
	return i, err
}

const deleteDependency = `-- name: DeleteDependency :execrows
DELETE FROM issue_dependencies
WHERE project_id = $1 AND blocked_id = $2 AND blocker_id = $3
`

type DeleteDependencyParams struct {
	ProjectID pgtype.UUID `json:"project_id"`
	BlockedID pgtype.UUID `json:"blocked_id"`
	BlockerID pgtype.UUID `json:"blocker_id"`
}

func (q *Queries) DeleteDependency(ctx context.Context, arg DeleteDependencyParams) (int64, error) {
	result, err := q.db.Exec(ctx, deleteDependency, arg.ProjectID, arg.BlockedID, arg.BlockerID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const listBlocked = `-- name: ListBlocked :many
SELECT i.id, i.project_id, i.requirement_id, i.number, i.title, i.description, i.status,
       i.assignee_id, i.created_by, i.closed_at, i.created_at, i.updated_at, i.workspace_id, i.parent_id, i.priority
FROM issue_dependencies d
JOIN issues i ON i.id = d.blocked_id
WHERE d.blocker_id = $1
ORDER BY i.number ASC
`

// Issues blocked by $1 (issues that depend on blocker $1).
func (q *Queries) ListBlocked(ctx context.Context, blockerID pgtype.UUID) ([]Issue, error) {
	rows, err := q.db.Query(ctx, listBlocked, blockerID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Issue
	for rows.Next() {
		var i Issue
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.RequirementID,
			&i.Number,
			&i.Title,
			&i.Description,
			&i.Status,
			&i.AssigneeID,
			&i.CreatedBy,
			&i.ClosedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.WorkspaceID,
			&i.ParentID,
			&i.Priority,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listBlockerIDs = `-- name: ListBlockerIDs :many
SELECT blocker_id FROM issue_dependencies WHERE blocked_id = $1
`

// Direct blocker issue ids of $1 (one hop along blocked-by edges). Used by the
// service-layer iterative reachability cycle check in AddDependency.
func (q *Queries) ListBlockerIDs(ctx context.Context, blockedID pgtype.UUID) ([]pgtype.UUID, error) {
	rows, err := q.db.Query(ctx, listBlockerIDs, blockedID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []pgtype.UUID
	for rows.Next() {
		var blocker_id pgtype.UUID
		if err := rows.Scan(&blocker_id); err != nil {
			return nil, err
		}
		items = append(items, blocker_id)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listBlockers = `-- name: ListBlockers :many
SELECT i.id, i.project_id, i.requirement_id, i.number, i.title, i.description, i.status,
       i.assignee_id, i.created_by, i.closed_at, i.created_at, i.updated_at, i.workspace_id, i.parent_id, i.priority
FROM issue_dependencies d
JOIN issues i ON i.id = d.blocker_id
WHERE d.blocked_id = $1
ORDER BY i.number ASC
`

// Issues that block $1 (the dependencies of blocked issue $1).
func (q *Queries) ListBlockers(ctx context.Context, blockedID pgtype.UUID) ([]Issue, error) {
	rows, err := q.db.Query(ctx, listBlockers, blockedID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Issue
	for rows.Next() {
		var i Issue
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.RequirementID,
			&i.Number,
			&i.Title,
			&i.Description,
			&i.Status,
			&i.AssigneeID,
			&i.CreatedBy,
			&i.ClosedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.WorkspaceID,
			&i.ParentID,
			&i.Priority,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listReadyLeafSubtasks = `-- name: ListReadyLeafSubtasks :many
SELECT i.id, i.project_id, i.requirement_id, i.number, i.title, i.description, i.status,
       i.assignee_id, i.created_by, i.closed_at, i.created_at, i.updated_at, i.workspace_id, i.parent_id, i.priority
FROM issues i
WHERE i.project_id = $1
  AND i.status = 'open'
  AND i.parent_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM issue_dependencies d
    JOIN issues b ON b.id = d.blocker_id
    WHERE d.blocked_id = i.id AND b.status <> 'closed'
  )
  AND NOT EXISTS (
    SELECT 1 FROM acp_sessions s
    WHERE s.issue_id = i.id AND s.status IN ('starting', 'running')
  )
  AND NOT EXISTS (
    SELECT 1 FROM issues c
    WHERE c.parent_id = i.id AND c.status = 'open'
  )
ORDER BY i.priority DESC, i.number ASC
LIMIT $2
`

type ListReadyLeafSubtasksParams struct {
	ProjectID pgtype.UUID `json:"project_id"`
	Limit     int32       `json:"limit"`
}

// Ready leaf subtasks for a project: open subtasks (parent_id NOT NULL) whose
// every blocker is closed, with no active (starting/running) acp_session, and
// no open child issue (leaf). Ordered by priority DESC, number ASC. LIMIT $2.
func (q *Queries) ListReadyLeafSubtasks(ctx context.Context, arg ListReadyLeafSubtasksParams) ([]Issue, error) {
	rows, err := q.db.Query(ctx, listReadyLeafSubtasks, arg.ProjectID, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Issue
	for rows.Next() {
		var i Issue
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.RequirementID,
			&i.Number,
			&i.Title,
			&i.Description,
			&i.Status,
			&i.AssigneeID,
			&i.CreatedBy,
			&i.ClosedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.WorkspaceID,
			&i.ParentID,
			&i.Priority,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listSchedulableProjects = `-- name: ListSchedulableProjects :many
SELECT DISTINCT i.project_id
FROM issues i
WHERE i.status = 'open'
  AND i.parent_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM issue_dependencies d
    JOIN issues b ON b.id = d.blocker_id
    WHERE d.blocked_id = i.id AND b.status <> 'closed'
  )
  AND NOT EXISTS (
    SELECT 1 FROM acp_sessions s
    WHERE s.issue_id = i.id AND s.status IN ('starting', 'running')
  )
  AND NOT EXISTS (
    SELECT 1 FROM issues c
    WHERE c.parent_id = i.id AND c.status = 'open'
  )
`

// Distinct project ids that currently have at least one ready leaf subtask
// (open subtask whose blockers are all closed and no active session). Cheap
// pre-filter so the scheduler only iterates projects with pending work.
func (q *Queries) ListSchedulableProjects(ctx context.Context) ([]pgtype.UUID, error) {
	rows, err := q.db.Query(ctx, listSchedulableProjects)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []pgtype.UUID
	for rows.Next() {
		var project_id pgtype.UUID
		if err := rows.Scan(&project_id); err != nil {
			return nil, err
		}
		items = append(items, project_id)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listSubtasks = `-- name: ListSubtasks :many
SELECT id, project_id, requirement_id, number, title, description, status,
       assignee_id, created_by, closed_at, created_at, updated_at, workspace_id, parent_id, priority
FROM issues
WHERE parent_id = $1
ORDER BY priority DESC, number ASC
`

func (q *Queries) ListSubtasks(ctx context.Context, parentID pgtype.UUID) ([]Issue, error) {
	rows, err := q.db.Query(ctx, listSubtasks, parentID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Issue
	for rows.Next() {
		var i Issue
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.RequirementID,
			&i.Number,
			&i.Title,
			&i.Description,
			&i.Status,
			&i.AssigneeID,
			&i.CreatedBy,
			&i.ClosedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.WorkspaceID,
			&i.ParentID,
			&i.Priority,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
