import { Body, Controller, Get, HttpStatus, Param, Post, Res, UseGuards, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse as SwaggerApiResponse, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { CurrentUser } from '../../gateway/auth/current_user.decorator';
import { JwtAuthGuard } from '../../gateway/auth/jwt_auth.guard';
import { JwtPayload } from '../../core/login_core/login_core.service';
import { ReportTaskActivityDto } from './dto/report_task_activity.dto';
import { TaskService } from './task.service';

@ApiTags('tasks')
@ApiBearerAuth()
@Controller('tasks')
@UseGuards(JwtAuthGuard)
export class TasksController {
  constructor(private readonly taskService: TaskService) {}

  @Get('board')
  @ApiOperation({ summary: '获取玩家任务书' })
  @SwaggerApiResponse({ status: 200, description: '任务书获取成功' })
  async getBoard(@CurrentUser() user: JwtPayload, @Res() res: Response): Promise<void> {
    const data = await this.taskService.getBoard(BigInt(user.sub));
    res.status(HttpStatus.OK).json({ success: true, data, message: '任务书获取成功' });
  }

  @Post('activities')
  @ApiOperation({ summary: '上报客户端白名单任务活动' })
  @ApiBody({ type: ReportTaskActivityDto })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async reportActivity(
    @CurrentUser() user: JwtPayload,
    @Body() dto: ReportTaskActivityDto,
    @Res() res: Response,
  ): Promise<void> {
    const data = await this.taskService.recordClientActivity(BigInt(user.sub), dto.activity, dto.target_id);
    res.status(HttpStatus.OK).json({ success: true, data, message: '任务进度已更新' });
  }

  @Post(':taskId/claim')
  @ApiOperation({ summary: '领取任务奖励' })
  async claim(
    @CurrentUser() user: JwtPayload,
    @Param('taskId') taskId: string,
    @Res() res: Response,
  ): Promise<void> {
    const data = await this.taskService.claim(BigInt(user.sub), taskId);
    res.status(HttpStatus.OK).json({ success: true, data, message: '任务奖励已领取' });
  }
}
