/**
 * AI Troubleshooting Step — generates troubleshooting steps and creates a ticket note.
 * Config: {
 *   template_purpose: 'troubleshooting_steps',
 *   create_note: boolean  // whether to create an Autotask ticket note
 * }
 * Condition: typically checks if ticket_type === 2 (Incident)
 */

import { registerWorkflowStepExecutor } from '../ticket-workflow-engine';
import { aiTriageService } from '../ai-triage-service';
import { AutotaskClient } from '../autotask-client';
import { TicketWorkflowStep, WorkflowStepContext, WorkflowStepResult } from '../../types/ticket-workflow';

async function executeAiTroubleshooting(
  step: TicketWorkflowStep,
  context: WorkflowStepContext,
  _executionId: number
): Promise<WorkflowStepResult> {
  const ticket = context.ticket;

  // Generate troubleshooting steps
  const troubleshootingSteps = await aiTriageService.generateTroubleshootingSteps(
    ticket,
    context._settings
  );

  if (!troubleshootingSteps) {
    return {
      success: true,
      output: {
        skipped: true,
        reason: 'No troubleshooting steps generated'
      }
    };
  }

  // Note: Ticket note creation would go here
  // For now, just return the troubleshooting steps in the output
  // TODO: Implement createTicketNote in AutotaskClient if needed

  return {
    success: true,
    output: {
      method: 'ai',
      troubleshooting_steps: troubleshootingSteps,
      note_created: false
    }
  };
}

registerWorkflowStepExecutor('ai_troubleshooting', executeAiTroubleshooting);
