import { TASK_EVENT_SCHEMA } from '../../lib/schema/events';
import { AGENT_TYPES, UserRole } from '../../lib/types/index';
import { logger } from '../../lib/logger';
import { Context } from 'aws-lambda';
import { handleRecursionLimitExceeded, processEventWithAgent } from './shared';
import { getRecursionLimit } from '../../lib/recursion-tracker';

/**
 * Handles continuation task events - resumes agent processing with context.
 *
 * @param eventDetail - The task event detail containing the agentId, task, and context.
 * @param context - The AWS Lambda context.
 */
export async function handleContinuationTask(
  eventDetail: Record<string, unknown>,
  context: Context
): Promise<void> {
  const {
    userId,
    agentId,
    task,
    traceId,
    taskId,
    sessionId,
    depth,
    initiatorId,
    attachments,
    tokenBudget,
    costLimit,
    metadata,
    workspaceId,
    teamId,
    staffId,
    userRole,
  } = TASK_EVENT_SCHEMA.parse(eventDetail);

  const currentDepth = depth ?? 1;

  // 1. Loop Protection - Check recursion depth before processing
  // Boundary: Depth 1 is the first level. If limit is 15, allow depths 1-15, block at 16+.
  // Must match the operator in events.ts (entry point) for consistent behavior.
  const RECURSION_LIMIT = await getRecursionLimit();

  if (currentDepth > RECURSION_LIMIT) {
    logger.error(
      `Recursion Limit Exceeded for CONTINUATION_TASK (Depth: ${currentDepth}) for user ${userId}. Aborting.`
    );
    await handleRecursionLimitExceeded(
      userId,
      sessionId,
      'continuation-handler',
      `I have detected an infinite loop in task continuation (Depth: ${currentDepth}). I've intervened to stop the process. Please check the orchestration logic.`,
      traceId,
      agentId ?? 'superclaw',
      workspaceId,
      teamId,
      staffId
    );
    return;
  }

  const priorTokenUsage = metadata?.priorTotalTokens
    ? {
        inputTokens: ((metadata as Record<string, unknown>).priorInputTokens as number) ?? 0,
        outputTokens: ((metadata as Record<string, unknown>).priorOutputTokens as number) ?? 0,
        totalTokens: ((metadata as Record<string, unknown>).priorTotalTokens as number) ?? 0,
      }
    : undefined;

  const targetAgentId = agentId ?? AGENT_TYPES.SUPERCLAW;
  logger.info(`Handling continuation task for agent ${targetAgentId}, user:`, userId, {
    traceId,
    sessionId,
    priorTokens: priorTokenUsage?.totalTokens ?? 0,
  });

  await processEventWithAgent(userId, targetAgentId, task, {
    context,
    isContinuation: true,
    traceId,
    taskId,
    sessionId,
    depth,
    initiatorId,
    attachments: attachments as import('../../lib/types/index').Attachment[],
    tokenBudget,
    costLimit,
    priorTokenUsage,
    handlerTitle: 'CONTINUATION_NOTIFICATION',
    outboundHandlerName: 'continuation-handler',
    workspaceId,
    teamId,
    staffId,
    userRole: userRole as UserRole,
  });
}
