Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

23.6 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: work_item_comment.sql

package store

import (
	"context"

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

const createWorkItemComment = `-- name: CreateWorkItemComment :one
INSERT INTO work_item_comments(
    id, work_item_id, tenant_id, author_actor_id, body, segments
)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, work_item_id, tenant_id, author_actor_id, body, segments,
          version, deleted_at, created_at, updated_at
`

type CreateWorkItemCommentParams struct {
	ID            string `json:"id"`
	WorkItemID    string `json:"work_item_id"`
	TenantID      string `json:"tenant_id"`
	AuthorActorID string `json:"author_actor_id"`
	Body          string `json:"body"`
	Segments      []byte `json:"segments"`
}

func (q *Queries) CreateWorkItemComment(ctx context.Context, arg CreateWorkItemCommentParams) (WorkItemComment, error) {
	row := q.db.QueryRow(ctx, createWorkItemComment,
		arg.ID,
		arg.WorkItemID,
		arg.TenantID,
		arg.AuthorActorID,
		arg.Body,
		arg.Segments,
	)
	var i WorkItemComment
	err := row.Scan(
		&i.ID,
		&i.WorkItemID,
		&i.TenantID,
		&i.AuthorActorID,
		&i.Body,
		&i.Segments,
		&i.Version,
		&i.DeletedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const createWorkItemCommentActivity = `-- name: CreateWorkItemCommentActivity :one
INSERT INTO work_item_activities(
    id, work_item_id, tenant_id, kind, actor_id, comment_id
)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, work_item_id, tenant_id, kind, actor_id, from_state, to_state,
          target_actor_id, comment_id, room_id, details, occurred_at
`

type CreateWorkItemCommentActivityParams struct {
	ID         string      `json:"id"`
	WorkItemID string      `json:"work_item_id"`
	TenantID   string      `json:"tenant_id"`
	Kind       string      `json:"kind"`
	ActorID    string      `json:"actor_id"`
	CommentID  pgtype.Text `json:"comment_id"`
}

type CreateWorkItemCommentActivityRow struct {
	ID            string             `json:"id"`
	WorkItemID    string             `json:"work_item_id"`
	TenantID      string             `json:"tenant_id"`
	Kind          string             `json:"kind"`
	ActorID       string             `json:"actor_id"`
	FromState     pgtype.Text        `json:"from_state"`
	ToState       pgtype.Text        `json:"to_state"`
	TargetActorID pgtype.Text        `json:"target_actor_id"`
	CommentID     pgtype.Text        `json:"comment_id"`
	RoomID        pgtype.Text        `json:"room_id"`
	Details       []byte             `json:"details"`
	OccurredAt    pgtype.Timestamptz `json:"occurred_at"`
}

func (q *Queries) CreateWorkItemCommentActivity(ctx context.Context, arg CreateWorkItemCommentActivityParams) (CreateWorkItemCommentActivityRow, error) {
	row := q.db.QueryRow(ctx, createWorkItemCommentActivity,
		arg.ID,
		arg.WorkItemID,
		arg.TenantID,
		arg.Kind,
		arg.ActorID,
		arg.CommentID,
	)
	var i CreateWorkItemCommentActivityRow
	err := row.Scan(
		&i.ID,
		&i.WorkItemID,
		&i.TenantID,
		&i.Kind,
		&i.ActorID,
		&i.FromState,
		&i.ToState,
		&i.TargetActorID,
		&i.CommentID,
		&i.RoomID,
		&i.Details,
		&i.OccurredAt,
	)
	return i, err
}

const createWorkItemCommentReference = `-- name: CreateWorkItemCommentReference :exec
INSERT INTO work_item_comment_references(comment_id, work_item_id, tenant_id, actor_id)
VALUES ($1, $2, $3, $4)
`

type CreateWorkItemCommentReferenceParams struct {
	CommentID  string `json:"comment_id"`
	WorkItemID string `json:"work_item_id"`
	TenantID   string `json:"tenant_id"`
	ActorID    string `json:"actor_id"`
}

func (q *Queries) CreateWorkItemCommentReference(ctx context.Context, arg CreateWorkItemCommentReferenceParams) error {
	_, err := q.db.Exec(ctx, createWorkItemCommentReference,
		arg.CommentID,
		arg.WorkItemID,
		arg.TenantID,
		arg.ActorID,
	)
	return err
}

const deleteWorkItemCommentReferences = `-- name: DeleteWorkItemCommentReferences :exec
DELETE FROM work_item_comment_references WHERE comment_id=$1
`

func (q *Queries) DeleteWorkItemCommentReferences(ctx context.Context, commentID string) error {
	_, err := q.db.Exec(ctx, deleteWorkItemCommentReferences, commentID)
	return err
}

const getActiveWorkItemAgentAuthority = `-- name: GetActiveWorkItemAgentAuthority :one
SELECT actor.id, actor.tenant_id, actor.kind, actor.display_name,
       actor.account_id, actor.handle, actor.created_at
FROM agent_registrations registration
JOIN actors actor
  ON actor.id=registration.actor_id
 AND actor.tenant_id=registration.tenant_id
 AND actor.kind='agent'
WHERE registration.agent_management_id=$1::text
  AND registration.status='active'
`

func (q *Queries) GetActiveWorkItemAgentAuthority(ctx context.Context, agentManagementID string) (Actor, error) {
	row := q.db.QueryRow(ctx, getActiveWorkItemAgentAuthority, agentManagementID)
	var i Actor
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.Kind,
		&i.DisplayName,
		&i.AccountID,
		&i.Handle,
		&i.CreatedAt,
	)
	return i, err
}

const getWorkItemActivity = `-- name: GetWorkItemActivity :one
SELECT id, work_item_id, tenant_id, kind, actor_id, from_state, to_state,
       target_actor_id, comment_id, room_id, details, occurred_at
FROM work_item_activities
WHERE id=$1 AND work_item_id=$2
`

type GetWorkItemActivityParams struct {
	ID         string `json:"id"`
	WorkItemID string `json:"work_item_id"`
}

type GetWorkItemActivityRow struct {
	ID            string             `json:"id"`
	WorkItemID    string             `json:"work_item_id"`
	TenantID      string             `json:"tenant_id"`
	Kind          string             `json:"kind"`
	ActorID       string             `json:"actor_id"`
	FromState     pgtype.Text        `json:"from_state"`
	ToState       pgtype.Text        `json:"to_state"`
	TargetActorID pgtype.Text        `json:"target_actor_id"`
	CommentID     pgtype.Text        `json:"comment_id"`
	RoomID        pgtype.Text        `json:"room_id"`
	Details       []byte             `json:"details"`
	OccurredAt    pgtype.Timestamptz `json:"occurred_at"`
}

func (q *Queries) GetWorkItemActivity(ctx context.Context, arg GetWorkItemActivityParams) (GetWorkItemActivityRow, error) {
	row := q.db.QueryRow(ctx, getWorkItemActivity, arg.ID, arg.WorkItemID)
	var i GetWorkItemActivityRow
	err := row.Scan(
		&i.ID,
		&i.WorkItemID,
		&i.TenantID,
		&i.Kind,
		&i.ActorID,
		&i.FromState,
		&i.ToState,
		&i.TargetActorID,
		&i.CommentID,
		&i.RoomID,
		&i.Details,
		&i.OccurredAt,
	)
	return i, err
}

const getWorkItemComment = `-- name: GetWorkItemComment :one
SELECT id, work_item_id, tenant_id, author_actor_id, body, segments,
       version, deleted_at, created_at, updated_at
FROM work_item_comments
WHERE id=$1 AND work_item_id=$2
`

type GetWorkItemCommentParams struct {
	ID         string `json:"id"`
	WorkItemID string `json:"work_item_id"`
}

func (q *Queries) GetWorkItemComment(ctx context.Context, arg GetWorkItemCommentParams) (WorkItemComment, error) {
	row := q.db.QueryRow(ctx, getWorkItemComment, arg.ID, arg.WorkItemID)
	var i WorkItemComment
	err := row.Scan(
		&i.ID,
		&i.WorkItemID,
		&i.TenantID,
		&i.AuthorActorID,
		&i.Body,
		&i.Segments,
		&i.Version,
		&i.DeletedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getWorkItemCommentForUpdate = `-- name: GetWorkItemCommentForUpdate :one
SELECT id, work_item_id, tenant_id, author_actor_id, body, segments,
       version, deleted_at, created_at, updated_at
FROM work_item_comments
WHERE id=$1 AND work_item_id=$2
FOR UPDATE
`

type GetWorkItemCommentForUpdateParams struct {
	ID         string `json:"id"`
	WorkItemID string `json:"work_item_id"`
}

func (q *Queries) GetWorkItemCommentForUpdate(ctx context.Context, arg GetWorkItemCommentForUpdateParams) (WorkItemComment, error) {
	row := q.db.QueryRow(ctx, getWorkItemCommentForUpdate, arg.ID, arg.WorkItemID)
	var i WorkItemComment
	err := row.Scan(
		&i.ID,
		&i.WorkItemID,
		&i.TenantID,
		&i.AuthorActorID,
		&i.Body,
		&i.Segments,
		&i.Version,
		&i.DeletedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getWorkItemForAgent = `-- name: GetWorkItemForAgent :one
SELECT wi.id, wi.tenant_id, wi.number, wi.kind, wi.title, wi.description, wi.priority,
       wi.state, wi.resolution, wi.version, wi.created_by_actor_id, wi.updated_by_actor_id,
       wi.archived_at, wi.created_at, wi.updated_at,
       wi.task_source_key, wi.task_execution_generation, wi.task_execution_status,
       wi.task_execution_revision, wi.task_execution_reason_code, wi.task_desired_control,
       wi.task_control_revision, wi.task_control_settled_revision, wi.task_control_outcome,
       wi.task_control_source_kind, wi.task_control_source_id, wi.task_control_requested_by_actor_id,
       wi.task_control_requested_at, wi.task_control_payload,
       wi.task_execution_updated_at, wi.task_next_reconcile_at, wi.task_worker_id,
       wi.task_lease_token_hash, wi.task_lease_generation, wi.task_lease_expires_at, wi.quiet
FROM work_items wi
WHERE wi.id=$1::text
  AND wi.tenant_id=$2::text
  AND (
      wi.created_by_actor_id=$3::text OR
      EXISTS (
          SELECT 1 FROM work_item_assignments assignment
          WHERE assignment.work_item_id=wi.id
            AND assignment.actor_id=$3::text
      ) OR
      EXISTS (
          SELECT 1 FROM work_item_description_references reference
          WHERE reference.work_item_id=wi.id
            AND reference.actor_id=$3::text
      ) OR
      EXISTS (
          SELECT 1 FROM work_item_comment_references reference
          WHERE reference.work_item_id=wi.id
            AND reference.actor_id=$3::text
      ) OR
      EXISTS (
          SELECT 1 FROM work_item_access_grants access_grant
          WHERE access_grant.work_item_id=wi.id
            AND access_grant.actor_id=$3::text
      )
  )
`

type GetWorkItemForAgentParams struct {
	ID           string `json:"id"`
	TenantID     string `json:"tenant_id"`
	AgentActorID string `json:"agent_actor_id"`
}

func (q *Queries) GetWorkItemForAgent(ctx context.Context, arg GetWorkItemForAgentParams) (WorkItem, error) {
	row := q.db.QueryRow(ctx, getWorkItemForAgent, arg.ID, arg.TenantID, arg.AgentActorID)
	var i WorkItem
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.Number,
		&i.Kind,
		&i.Title,
		&i.Description,
		&i.Priority,
		&i.State,
		&i.Resolution,
		&i.Version,
		&i.CreatedByActorID,
		&i.UpdatedByActorID,
		&i.ArchivedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.TaskSourceKey,
		&i.TaskExecutionGeneration,
		&i.TaskExecutionStatus,
		&i.TaskExecutionRevision,
		&i.TaskExecutionReasonCode,
		&i.TaskDesiredControl,
		&i.TaskControlRevision,
		&i.TaskControlSettledRevision,
		&i.TaskControlOutcome,
		&i.TaskControlSourceKind,
		&i.TaskControlSourceID,
		&i.TaskControlRequestedByActorID,
		&i.TaskControlRequestedAt,
		&i.TaskControlPayload,
		&i.TaskExecutionUpdatedAt,
		&i.TaskNextReconcileAt,
		&i.TaskWorkerID,
		&i.TaskLeaseTokenHash,
		&i.TaskLeaseGeneration,
		&i.TaskLeaseExpiresAt,
		&i.Quiet,
	)
	return i, err
}

const getWorkItemForAgentForUpdate = `-- name: GetWorkItemForAgentForUpdate :one
SELECT wi.id, wi.tenant_id, wi.number, wi.kind, wi.title, wi.description, wi.priority,
       wi.state, wi.resolution, wi.version, wi.created_by_actor_id, wi.updated_by_actor_id,
       wi.archived_at, wi.created_at, wi.updated_at,
       wi.task_source_key, wi.task_execution_generation, wi.task_execution_status,
       wi.task_execution_revision, wi.task_execution_reason_code, wi.task_desired_control,
       wi.task_control_revision, wi.task_control_settled_revision, wi.task_control_outcome,
       wi.task_control_source_kind, wi.task_control_source_id, wi.task_control_requested_by_actor_id,
       wi.task_control_requested_at, wi.task_control_payload,
       wi.task_execution_updated_at, wi.task_next_reconcile_at, wi.task_worker_id,
       wi.task_lease_token_hash, wi.task_lease_generation, wi.task_lease_expires_at, wi.quiet
FROM work_items wi
WHERE wi.id=$1::text
  AND wi.tenant_id=$2::text
  AND (
      wi.created_by_actor_id=$3::text OR
      EXISTS (SELECT 1 FROM work_item_assignments a WHERE a.work_item_id=wi.id AND a.actor_id=$3::text) OR
      EXISTS (SELECT 1 FROM work_item_description_references r WHERE r.work_item_id=wi.id AND r.actor_id=$3::text) OR
      EXISTS (SELECT 1 FROM work_item_comment_references r WHERE r.work_item_id=wi.id AND r.actor_id=$3::text) OR
      EXISTS (SELECT 1 FROM work_item_access_grants g WHERE g.work_item_id=wi.id AND g.actor_id=$3::text)
  )
FOR UPDATE OF wi
`

type GetWorkItemForAgentForUpdateParams struct {
	ID           string `json:"id"`
	TenantID     string `json:"tenant_id"`
	AgentActorID string `json:"agent_actor_id"`
}

func (q *Queries) GetWorkItemForAgentForUpdate(ctx context.Context, arg GetWorkItemForAgentForUpdateParams) (WorkItem, error) {
	row := q.db.QueryRow(ctx, getWorkItemForAgentForUpdate, arg.ID, arg.TenantID, arg.AgentActorID)
	var i WorkItem
	err := row.Scan(
		&i.ID,
		&i.TenantID,
		&i.Number,
		&i.Kind,
		&i.Title,
		&i.Description,
		&i.Priority,
		&i.State,
		&i.Resolution,
		&i.Version,
		&i.CreatedByActorID,
		&i.UpdatedByActorID,
		&i.ArchivedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.TaskSourceKey,
		&i.TaskExecutionGeneration,
		&i.TaskExecutionStatus,
		&i.TaskExecutionRevision,
		&i.TaskExecutionReasonCode,
		&i.TaskDesiredControl,
		&i.TaskControlRevision,
		&i.TaskControlSettledRevision,
		&i.TaskControlOutcome,
		&i.TaskControlSourceKind,
		&i.TaskControlSourceID,
		&i.TaskControlRequestedByActorID,
		&i.TaskControlRequestedAt,
		&i.TaskControlPayload,
		&i.TaskExecutionUpdatedAt,
		&i.TaskNextReconcileAt,
		&i.TaskWorkerID,
		&i.TaskLeaseTokenHash,
		&i.TaskLeaseGeneration,
		&i.TaskLeaseExpiresAt,
		&i.Quiet,
	)
	return i, err
}

const isWorkItemAssignedToActor = `-- name: IsWorkItemAssignedToActor :one
SELECT EXISTS (
    SELECT 1 FROM work_item_assignments
    WHERE work_item_id=$1 AND actor_id=$2
)
`

type IsWorkItemAssignedToActorParams struct {
	WorkItemID string `json:"work_item_id"`
	ActorID    string `json:"actor_id"`
}

func (q *Queries) IsWorkItemAssignedToActor(ctx context.Context, arg IsWorkItemAssignedToActorParams) (bool, error) {
	row := q.db.QueryRow(ctx, isWorkItemAssignedToActor, arg.WorkItemID, arg.ActorID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const listWorkItemCommentReferenceActorIDs = `-- name: ListWorkItemCommentReferenceActorIDs :many
SELECT actor_id
FROM work_item_comment_references
WHERE comment_id=$1
ORDER BY actor_id
`

func (q *Queries) ListWorkItemCommentReferenceActorIDs(ctx context.Context, commentID string) ([]string, error) {
	rows, err := q.db.Query(ctx, listWorkItemCommentReferenceActorIDs, commentID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []string{}
	for rows.Next() {
		var actor_id string
		if err := rows.Scan(&actor_id); err != nil {
			return nil, err
		}
		items = append(items, actor_id)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listWorkItemComments = `-- name: ListWorkItemComments :many
SELECT id, work_item_id, tenant_id, author_actor_id, body, segments,
       version, deleted_at, created_at, updated_at
FROM work_item_comments
WHERE work_item_id=$1::text
  AND (
      NOT $2::boolean OR
      (created_at, id) > ($3::timestamptz, $4::text)
  )
ORDER BY created_at, id
LIMIT $5::int
`

type ListWorkItemCommentsParams struct {
	WorkItemID      string             `json:"work_item_id"`
	HasCursor       bool               `json:"has_cursor"`
	CursorCreatedAt pgtype.Timestamptz `json:"cursor_created_at"`
	CursorID        string             `json:"cursor_id"`
	PageLimit       int32              `json:"page_limit"`
}

func (q *Queries) ListWorkItemComments(ctx context.Context, arg ListWorkItemCommentsParams) ([]WorkItemComment, error) {
	rows, err := q.db.Query(ctx, listWorkItemComments,
		arg.WorkItemID,
		arg.HasCursor,
		arg.CursorCreatedAt,
		arg.CursorID,
		arg.PageLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []WorkItemComment{}
	for rows.Next() {
		var i WorkItemComment
		if err := rows.Scan(
			&i.ID,
			&i.WorkItemID,
			&i.TenantID,
			&i.AuthorActorID,
			&i.Body,
			&i.Segments,
			&i.Version,
			&i.DeletedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listWorkItemTimelineRefs = `-- name: ListWorkItemTimelineRefs :many
SELECT entry_type, entry_id, occurred_at
FROM (
    SELECT 'activity'::text AS entry_type, id AS entry_id, occurred_at
    FROM work_item_activities
    WHERE work_item_id=$1::text
    UNION ALL
    SELECT 'comment'::text AS entry_type, id AS entry_id, created_at AS occurred_at
    FROM work_item_comments
    WHERE work_item_id=$1::text
) timeline
WHERE (
    NOT $2::boolean OR
    (occurred_at, entry_id) > ($3::timestamptz, $4::text)
)
ORDER BY occurred_at, entry_id
LIMIT $5::int
`

type ListWorkItemTimelineRefsParams struct {
	WorkItemID       string             `json:"work_item_id"`
	HasCursor        bool               `json:"has_cursor"`
	CursorOccurredAt pgtype.Timestamptz `json:"cursor_occurred_at"`
	CursorID         string             `json:"cursor_id"`
	PageLimit        int32              `json:"page_limit"`
}

type ListWorkItemTimelineRefsRow struct {
	EntryType  string             `json:"entry_type"`
	EntryID    string             `json:"entry_id"`
	OccurredAt pgtype.Timestamptz `json:"occurred_at"`
}

func (q *Queries) ListWorkItemTimelineRefs(ctx context.Context, arg ListWorkItemTimelineRefsParams) ([]ListWorkItemTimelineRefsRow, error) {
	rows, err := q.db.Query(ctx, listWorkItemTimelineRefs,
		arg.WorkItemID,
		arg.HasCursor,
		arg.CursorOccurredAt,
		arg.CursorID,
		arg.PageLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListWorkItemTimelineRefsRow{}
	for rows.Next() {
		var i ListWorkItemTimelineRefsRow
		if err := rows.Scan(&i.EntryType, &i.EntryID, &i.OccurredAt); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listWorkItemsForAgent = `-- name: ListWorkItemsForAgent :many
SELECT wi.id, wi.tenant_id, wi.number, wi.kind, wi.title, wi.description, wi.priority,
       wi.state, wi.resolution, wi.version, wi.created_by_actor_id, wi.updated_by_actor_id,
       wi.archived_at, wi.created_at, wi.updated_at,
       wi.task_source_key, wi.task_execution_generation, wi.task_execution_status,
       wi.task_execution_revision, wi.task_execution_reason_code, wi.task_desired_control,
       wi.task_control_revision, wi.task_control_settled_revision, wi.task_control_outcome,
       wi.task_control_source_kind, wi.task_control_source_id, wi.task_control_requested_by_actor_id,
       wi.task_control_requested_at, wi.task_control_payload,
       wi.task_execution_updated_at, wi.task_next_reconcile_at, wi.task_worker_id,
       wi.task_lease_token_hash, wi.task_lease_generation, wi.task_lease_expires_at, wi.quiet
FROM work_items wi
WHERE wi.tenant_id=$1::text
  AND ($2::text = '' OR wi.kind=$2::text)
  AND ($3::text = '' OR wi.state=$3::text)
  AND ($4::boolean OR wi.archived_at IS NULL)
  AND (
      $5::text = '' OR EXISTS (
          SELECT 1 FROM work_item_room_links link
          WHERE link.work_item_id=wi.id
            AND link.tenant_id=wi.tenant_id
            AND link.link_type='work'
            AND link.room_id=$5::text
      )
  )
  AND (
      wi.created_by_actor_id=$6::text OR
      EXISTS (SELECT 1 FROM work_item_assignments a WHERE a.work_item_id=wi.id AND a.actor_id=$6::text) OR
      EXISTS (SELECT 1 FROM work_item_description_references r WHERE r.work_item_id=wi.id AND r.actor_id=$6::text) OR
      EXISTS (SELECT 1 FROM work_item_comment_references r WHERE r.work_item_id=wi.id AND r.actor_id=$6::text) OR
      EXISTS (SELECT 1 FROM work_item_access_grants g WHERE g.work_item_id=wi.id AND g.actor_id=$6::text)
  )
  AND (
      NOT $7::boolean OR
      (wi.updated_at, wi.id) < ($8::timestamptz, $9::text)
  )
ORDER BY wi.updated_at DESC, wi.id DESC
LIMIT $10::int
`

type ListWorkItemsForAgentParams struct {
	TenantID        string             `json:"tenant_id"`
	KindFilter      string             `json:"kind_filter"`
	StateFilter     string             `json:"state_filter"`
	IncludeArchived bool               `json:"include_archived"`
	WorkRoomID      string             `json:"work_room_id"`
	AgentActorID    string             `json:"agent_actor_id"`
	HasCursor       bool               `json:"has_cursor"`
	CursorUpdatedAt pgtype.Timestamptz `json:"cursor_updated_at"`
	CursorID        string             `json:"cursor_id"`
	PageLimit       int32              `json:"page_limit"`
}

func (q *Queries) ListWorkItemsForAgent(ctx context.Context, arg ListWorkItemsForAgentParams) ([]WorkItem, error) {
	rows, err := q.db.Query(ctx, listWorkItemsForAgent,
		arg.TenantID,
		arg.KindFilter,
		arg.StateFilter,
		arg.IncludeArchived,
		arg.WorkRoomID,
		arg.AgentActorID,
		arg.HasCursor,
		arg.CursorUpdatedAt,
		arg.CursorID,
		arg.PageLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []WorkItem{}
	for rows.Next() {
		var i WorkItem
		if err := rows.Scan(
			&i.ID,
			&i.TenantID,
			&i.Number,
			&i.Kind,
			&i.Title,
			&i.Description,
			&i.Priority,
			&i.State,
			&i.Resolution,
			&i.Version,
			&i.CreatedByActorID,
			&i.UpdatedByActorID,
			&i.ArchivedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.TaskSourceKey,
			&i.TaskExecutionGeneration,
			&i.TaskExecutionStatus,
			&i.TaskExecutionRevision,
			&i.TaskExecutionReasonCode,
			&i.TaskDesiredControl,
			&i.TaskControlRevision,
			&i.TaskControlSettledRevision,
			&i.TaskControlOutcome,
			&i.TaskControlSourceKind,
			&i.TaskControlSourceID,
			&i.TaskControlRequestedByActorID,
			&i.TaskControlRequestedAt,
			&i.TaskControlPayload,
			&i.TaskExecutionUpdatedAt,
			&i.TaskNextReconcileAt,
			&i.TaskWorkerID,
			&i.TaskLeaseTokenHash,
			&i.TaskLeaseGeneration,
			&i.TaskLeaseExpiresAt,
			&i.Quiet,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const softDeleteWorkItemComment = `-- name: SoftDeleteWorkItemComment :one
UPDATE work_item_comments
SET body='', segments='[]'::jsonb, version=version+1, deleted_at=now(), updated_at=now()
WHERE id=$1 AND work_item_id=$2 AND deleted_at IS NULL
RETURNING id, work_item_id, tenant_id, author_actor_id, body, segments,
          version, deleted_at, created_at, updated_at
`

type SoftDeleteWorkItemCommentParams struct {
	ID         string `json:"id"`
	WorkItemID string `json:"work_item_id"`
}

func (q *Queries) SoftDeleteWorkItemComment(ctx context.Context, arg SoftDeleteWorkItemCommentParams) (WorkItemComment, error) {
	row := q.db.QueryRow(ctx, softDeleteWorkItemComment, arg.ID, arg.WorkItemID)
	var i WorkItemComment
	err := row.Scan(
		&i.ID,
		&i.WorkItemID,
		&i.TenantID,
		&i.AuthorActorID,
		&i.Body,
		&i.Segments,
		&i.Version,
		&i.DeletedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const updateWorkItemComment = `-- name: UpdateWorkItemComment :one
UPDATE work_item_comments
SET body=$3, segments=$4, version=version+1, updated_at=now()
WHERE id=$1 AND work_item_id=$2 AND deleted_at IS NULL
RETURNING id, work_item_id, tenant_id, author_actor_id, body, segments,
          version, deleted_at, created_at, updated_at
`

type UpdateWorkItemCommentParams struct {
	ID         string `json:"id"`
	WorkItemID string `json:"work_item_id"`
	Body       string `json:"body"`
	Segments   []byte `json:"segments"`
}

func (q *Queries) UpdateWorkItemComment(ctx context.Context, arg UpdateWorkItemCommentParams) (WorkItemComment, error) {
	row := q.db.QueryRow(ctx, updateWorkItemComment,
		arg.ID,
		arg.WorkItemID,
		arg.Body,
		arg.Segments,
	)
	var i WorkItemComment
	err := row.Scan(
		&i.ID,
		&i.WorkItemID,
		&i.TenantID,
		&i.AuthorActorID,
		&i.Body,
		&i.Segments,
		&i.Version,
		&i.DeletedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}