import { HEALTH_REPORT_EVENT_SCHEMA } from '../../lib/schema/events';
import { AGENT_TYPES, UserRole } from '../../lib/types/agent';
import { Context } from 'aws-lambda';
import { processEventWithAgent } from './shared';

/**
 * Handles system health report events - triggers agent to investigate issues.
 *
 * @param eventDetail - The health report event detail.
 * @param context - The AWS Lambda context.
 * @returns A promise resolving when the health report is processed.
 */
export async function handleHealthReport(
  eventDetail: Record<string, unknown>,
  context: Context
): Promise<void> {
  const {
    component,
    issue,
    severity,
    context: issueContext,
    userId,
    traceId,
    sessionId,
    workspaceId,
    teamId,
    staffId,
    userRole,
  } = HEALTH_REPORT_EVENT_SCHEMA.parse(eventDetail);

  const triageTask = `SYSTEM HEALTH ALERT: A component has reported an internal issue.
    
    Component: ${component}
    Issue: ${issue}
    Severity: ${severity.toUpperCase()}
    
    Context:
    ${JSON.stringify(issueContext ?? {}, null, 2)}
    
    Please investigate this health issue. Determine if it requires a code modification (Coder Agent), configuration change, or if it can be resolved via an autonomous recovery action.
    Start by diagnosing the root cause using your tools.`;

  await processEventWithAgent(userId, AGENT_TYPES.SUPERCLAW, triageTask, {
    context,
    traceId,
    sessionId,
    handlerTitle: 'HEALTH_TRIAGE',
    outboundHandlerName: 'health-handler',
    formatResponse: (responseText) =>
      `🚨 **SYSTEM HEALTH ALERT** (CRITICAL)
Component: ${component}
Issue: ${issue}

SuperClaw response: ${responseText}`,
    workspaceId,
    teamId,
    staffId,
    userRole: userRole as UserRole,
  });
}
