import { Provide, Inject, Post, Body, Controller, Logger } from '@midwayjs/core';
import { ILogger } from '@midwayjs/logger';
import { Context } from '@midwayjs/koa';
import { SkillLoaderService } from '../service/skill_loader.js';
import { NetaClawAgentExecutorService } from '../service/agent_executor.js';

@Provide()
@Controller('/open/netaclaw')
export class NetaClawChatController {
  @Inject()
  ctx: Context;

  @Logger()
  logger: ILogger;

  @Inject()
  skillLoader: SkillLoaderService;

  @Inject()
  agentExecutor: NetaClawAgentExecutorService;

  @Post('/chat')
  async chat(@Body() body: { sessionId?: string; message: string; agentName?: string; userId?: string }) {
    const result = await this.agentExecutor.execute({
      sessionId: body.sessionId,
      message: body.message,
      agentName: body.agentName,
      userId: body.userId,
    });

    return {
      sessionId: result.sessionId,
      content: result.content,
      usage: result.usage,
      toolCallCount: result.toolCallCount,
      assistantEntryId: result.assistantEntryId,
      subagentBatches: result.subagentBatches,
    };
  }

  @Post('/skills')
  async listSkills() {
    return this.skillLoader.getAllSkills().map(s => ({ name: s.name, description: s.description }));
  }
}
