import { Injectable, Logger } from '@nestjs/common';
import {
  AgentEnginePort,
  EngineTaskParams,
  EngineStreamEvent,
} from '../../../domain/ports/outbound/agent-engine.port';
import { AgentEngineType } from '../../../domain/value-objects/agent-engine-type.vo';

/**
 * Placeholder custom engine implementation.
 * Extend this class to integrate a custom AI model or workflow
 * as an agent engine within the IT0 platform.
 */
@Injectable()
export class CustomEngine implements AgentEnginePort {
  readonly engineType = AgentEngineType.CUSTOM;
  private readonly logger = new Logger(CustomEngine.name);

  async *executeTask(params: EngineTaskParams): AsyncGenerator<EngineStreamEvent> {
    this.logger.log(
      `Custom engine executeTask called for session ${params.sessionId}`,
    );

    yield {
      type: 'text',
      content:
        'Custom engine is a placeholder. Implement your own logic by extending this class.',
    };

    yield {
      type: 'completed',
      summary: 'Custom engine placeholder completed',
      tokensUsed: 0,
    };
  }

  async cancelTask(sessionId: string): Promise<void> {
    this.logger.log(`Custom engine cancelTask called for session ${sessionId}`);
    // No-op for placeholder
  }

  async *continueSession(
    sessionId: string,
    message: string,
    _conversationHistory?: Array<{ role: 'user' | 'assistant'; content: string | any[] }>,
  ): AsyncGenerator<EngineStreamEvent> {
    this.logger.log(
      `Custom engine continueSession called for session ${sessionId}`,
    );

    yield {
      type: 'text',
      content: `Custom engine received message: ${message}`,
    };

    yield {
      type: 'completed',
      summary: 'Custom engine placeholder session continued',
      tokensUsed: 0,
    };
  }

  async healthCheck(): Promise<boolean> {
    // Custom engine is always "healthy" as a placeholder
    return true;
  }
}
