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

package db

import (
	"context"

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

const countAssigneeChangesByActor = `-- name: CountAssigneeChangesByActor :many
SELECT
  details->>'to_type' as assignee_type,
  details->>'to_id' as assignee_id,
  COUNT(*)::bigint as frequency
FROM activity_log
WHERE workspace_id = $1
  AND actor_id = $2
  AND actor_type = 'member'
  AND action = 'assignee_changed'
  AND details->>'to_type' IS NOT NULL
  AND details->>'to_id' IS NOT NULL
GROUP BY details->>'to_type', details->>'to_id'
`

type CountAssigneeChangesByActorParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	ActorID     pgtype.UUID `json:"actor_id"`
}

type CountAssigneeChangesByActorRow struct {
	AssigneeType interface{} `json:"assignee_type"`
	AssigneeID   interface{} `json:"assignee_id"`
	Frequency    int64       `json:"frequency"`
}

// Count how many times a user assigned each target via assignee_changed activities.
func (q *Queries) CountAssigneeChangesByActor(ctx context.Context, arg CountAssigneeChangesByActorParams) ([]CountAssigneeChangesByActorRow, error) {
	rows, err := q.db.Query(ctx, countAssigneeChangesByActor, arg.WorkspaceID, arg.ActorID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []CountAssigneeChangesByActorRow{}
	for rows.Next() {
		var i CountAssigneeChangesByActorRow
		if err := rows.Scan(&i.AssigneeType, &i.AssigneeID, &i.Frequency); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const createActivity = `-- name: CreateActivity :one
INSERT INTO activity_log (
    workspace_id, issue_id, actor_type, actor_id, action, details
) VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, workspace_id, issue_id, actor_type, actor_id, action, details, created_at
`

type CreateActivityParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	IssueID     pgtype.UUID `json:"issue_id"`
	ActorType   pgtype.Text `json:"actor_type"`
	ActorID     pgtype.UUID `json:"actor_id"`
	Action      string      `json:"action"`
	Details     []byte      `json:"details"`
}

func (q *Queries) CreateActivity(ctx context.Context, arg CreateActivityParams) (ActivityLog, error) {
	row := q.db.QueryRow(ctx, createActivity,
		arg.WorkspaceID,
		arg.IssueID,
		arg.ActorType,
		arg.ActorID,
		arg.Action,
		arg.Details,
	)
	var i ActivityLog
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.IssueID,
		&i.ActorType,
		&i.ActorID,
		&i.Action,
		&i.Details,
		&i.CreatedAt,
	)
	return i, err
}

const getActivity = `-- name: GetActivity :one
SELECT id, workspace_id, issue_id, actor_type, actor_id, action, details, created_at FROM activity_log
WHERE id = $1
`

func (q *Queries) GetActivity(ctx context.Context, id pgtype.UUID) (ActivityLog, error) {
	row := q.db.QueryRow(ctx, getActivity, id)
	var i ActivityLog
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.IssueID,
		&i.ActorType,
		&i.ActorID,
		&i.Action,
		&i.Details,
		&i.CreatedAt,
	)
	return i, err
}

const hasSquadLeaderNoActionEvaluationForTask = `-- name: HasSquadLeaderNoActionEvaluationForTask :one
SELECT EXISTS (
  SELECT 1
  FROM activity_log
  WHERE issue_id = $1
    AND actor_type = 'agent'
    AND actor_id = $2
    AND action = 'squad_leader_evaluated'
    AND details->>'outcome' = 'no_action'
    AND details->>'task_id' = $3::text
) AS exists
`

type HasSquadLeaderNoActionEvaluationForTaskParams struct {
	IssueID pgtype.UUID `json:"issue_id"`
	AgentID pgtype.UUID `json:"agent_id"`
	TaskID  string      `json:"task_id"`
}

func (q *Queries) HasSquadLeaderNoActionEvaluationForTask(ctx context.Context, arg HasSquadLeaderNoActionEvaluationForTaskParams) (bool, error) {
	row := q.db.QueryRow(ctx, hasSquadLeaderNoActionEvaluationForTask, arg.IssueID, arg.AgentID, arg.TaskID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const listActivitiesForIssue = `-- name: ListActivitiesForIssue :many
SELECT id, workspace_id, issue_id, actor_type, actor_id, action, details, created_at FROM activity_log
WHERE issue_id = $1
ORDER BY created_at ASC, id ASC
LIMIT $2
`

type ListActivitiesForIssueParams struct {
	IssueID pgtype.UUID `json:"issue_id"`
	Limit   int32       `json:"limit"`
}

// All activities for an issue in chronological order, capped at $2 (DB safety
// net to bound the response).
func (q *Queries) ListActivitiesForIssue(ctx context.Context, arg ListActivitiesForIssueParams) ([]ActivityLog, error) {
	rows, err := q.db.Query(ctx, listActivitiesForIssue, arg.IssueID, arg.Limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ActivityLog{}
	for rows.Next() {
		var i ActivityLog
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.IssueID,
			&i.ActorType,
			&i.ActorID,
			&i.Action,
			&i.Details,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
