import { Body, Controller, Delete, Get, Param, Patch, Post, UseGuards, ValidationPipe } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { TestLabService } from './test_lab.service';
import { CreateTestLabActorDto, TestLabMessageDto, TestLabRoomPolicyDto, TestLabSocialActionDto, UpdateTestLabActorDto } from './test_lab.dto';
import { Req } from '@nestjs/common';
import { TestLabGuard, TestLabRequest } from './test_lab.guard';

@ApiTags('admin-test-lab')
@ApiBearerAuth('JWT-auth')
@UseGuards(TestLabGuard)
@Controller('admin/test-lab')
export class TestLabController {
  constructor(private readonly testLab: TestLabService) {}

  @Get('status')
  @ApiOperation({ summary: '获取测试实验室状态' })
  async status() {
    return { success: true, data: await this.testLab.status() };
  }

  @Post('actors')
  @ApiOperation({ summary: '创建并上线测试假人' })
  async create(@Body(new ValidationPipe({ transform: true })) dto: CreateTestLabActorDto, @Req() request: TestLabRequest) {
    return { success: true, data: await this.testLab.createActor(dto, this.admin(request)) };
  }

  @Patch('actors/:userId')
  @ApiOperation({ summary: '更新测试假人的在线、位置或外观' })
  async update(@Param('userId') userId: string, @Body(new ValidationPipe({ transform: true })) dto: UpdateTestLabActorDto, @Req() request: TestLabRequest) {
    return { success: true, data: await this.testLab.updateActor(userId, dto, this.admin(request)) };
  }

  @Patch('actors/:userId/room-policy')
  @ApiOperation({ summary: '设置测试假人房间访问策略' })
  async roomPolicy(@Param('userId') userId: string, @Body(new ValidationPipe({ transform: true })) dto: TestLabRoomPolicyDto, @Req() request: TestLabRequest) {
    return { success: true, data: await this.testLab.setRoomPolicy(userId, dto, this.admin(request)) };
  }

  @Post('actors/:userId/messages')
  @ApiOperation({ summary: '以测试假人身份发送公共消息或私聊' })
  async message(@Param('userId') userId: string, @Body(new ValidationPipe({ transform: true })) dto: TestLabMessageDto, @Req() request: TestLabRequest) {
    return { success: true, data: await this.testLab.sendMessage(userId, dto, this.admin(request)) };
  }

  @Post('actors/:userId/social')
  @ApiOperation({ summary: '以测试假人身份执行好友或拉黑操作' })
  async social(@Param('userId') userId: string, @Body(new ValidationPipe({ transform: true })) dto: TestLabSocialActionDto, @Req() request: TestLabRequest) {
    return { success: true, data: await this.testLab.socialAction(userId, dto, this.admin(request)) };
  }

  @Delete('actors/:userId')
  @ApiOperation({ summary: '删除单个测试假人及其测试数据' })
  async remove(@Param('userId') userId: string, @Req() request: TestLabRequest) {
    return { success: true, data: await this.testLab.removeActor(userId, this.admin(request)) };
  }

  @Delete('actors')
  @ApiOperation({ summary: '清空全部测试假人及关联测试数据' })
  async clear(@Req() request: TestLabRequest) {
    return { success: true, data: await this.testLab.clear(this.admin(request)) };
  }

  private admin(request: TestLabRequest) {
    if (!request.admin) throw new Error('管理员身份缺失');
    return request.admin;
  }
}
