import { EngineStreamEvent } from '../../domain/ports/outbound/agent-engine.port';

/**
 * Client-facing stream event format.
 * Maps internal EngineStreamEvent to a normalized shape for WebSocket consumers.
 */
export class StreamEventDto {
  type!: string;
  timestamp!: string;
  data!: Record<string, unknown>;

  static fromEngineEvent(event: EngineStreamEvent): StreamEventDto {
    const dto = new StreamEventDto();
    dto.type = event.type;
    dto.timestamp = new Date().toISOString();

    switch (event.type) {
      case 'thinking':
        dto.data = { content: event.content };
        break;
      case 'text':
        dto.data = { content: event.content };
        break;
      case 'tool_use':
        dto.data = { toolName: event.toolName, input: event.input };
        break;
      case 'tool_result':
        dto.data = {
          toolName: event.toolName,
          output: event.output,
          isError: event.isError,
        };
        break;
      case 'approval_required':
        dto.data = {
          command: event.command,
          riskLevel: event.riskLevel,
          taskId: event.taskId,
        };
        break;
      case 'completed':
        dto.data = {
          summary: event.summary,
          tokensUsed: event.tokensUsed,
        };
        break;
      case 'error':
        dto.data = {
          message: event.message,
          code: event.code,
        };
        break;
      default:
        dto.data = {};
    }

    return dto;
  }
}
