import { Controller, Get, Post, Param, Body, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiQuery, ApiBody } from '@nestjs/swagger';
import { PrismaService } from '../../infrastructure/database/prisma.service';
import { AdminAuthGuard } from '../../common/guards/admin-auth.guard';
import { AdminRolesGuard } from '../../common/guards/admin-roles.guard';

@ApiTags('admin-hermes')
@ApiBearerAuth()
@Controller('admin-api/hermes')
@UseGuards(AdminAuthGuard, AdminRolesGuard)
export class HermesAgentController {
  constructor(private readonly prisma: PrismaService) {}

  @Get('tasks')
  @ApiOperation({ summary: 'Agent 任务列表' })
  @ApiQuery({ name: 'status', required: false })
  async listTasks(@Query('status') status?: string) {
    const where: any = {};
    if (status) where.status = status;
    return this.prisma.agentTask.findMany({ where, orderBy: { createdAt: 'desc' }, take: 100 });
  }

  @Post('tasks/:id/approve')
  @ApiOperation({ summary: '审批通过 Agent 任务' })
  async approveTask(@Param('id') id: string) {
    return this.prisma.agentTask.update({
      where: { id },
      data: { status: 'approved', approvedAt: new Date() },
    });
  }

  @Post('tasks/:id/reject')
  @ApiOperation({ summary: '驳回 Agent 任务' })
  async rejectTask(@Param('id') id: string) {
    return this.prisma.agentTask.update({
      where: { id },
      data: { status: 'rejected' },
    });
  }

  @Get('artifacts')
  @ApiOperation({ summary: 'Agent 产出物列表' })
  @ApiQuery({ name: 'type', required: false })
  async listArtifacts(@Query('type') type?: string) {
    const where: any = {};
    if (type) where.type = type;
    return this.prisma.agentArtifact.findMany({ where, orderBy: { createdAt: 'desc' }, take: 100 });
  }
}
