import { Controller, Get, Post, Param, Query, Body, Req, UseGuards, ParseIntPipe } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { PracticesService } from './practices.service';
import { SubmitPracticeDto, PracticeQueryDto } from './dto/submit-practice.dto';

@ApiTags('练习')
@Controller('practices')
export class PracticesController {
  constructor(private practicesService: PracticesService) {}

  @Get()
  findAll(@Query() query: PracticeQueryDto) {
    return this.practicesService.findAll(query);
  }

  @Get('categories')
  getCategories() {
    return this.practicesService.getCategories();
  }

  @Get('difficulties')
  getDifficulties() {
    return this.practicesService.getDifficulties();
  }

  @Get('submissions')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  getUserSubmissions(
    @Req() req: any,
    @Query('page') page?: number,
    @Query('limit') limit?: number,
  ) {
    return this.practicesService.getUserSubmissions(req.user.userId, page, limit);
  }

  @Get('submissions/:id')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  getSubmissionDetail(@Req() req: any, @Param('id', ParseIntPipe) id: number) {
    return this.practicesService.getSubmissionDetail(req.user.userId, id);
  }

  @Get(':id')
  findById(@Param('id', ParseIntPipe) id: number) {
    return this.practicesService.findById(id);
  }

  @Post(':id/submit')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  submitAnswer(
    @Req() req: any,
    @Param('id', ParseIntPipe) id: number,
    @Body() body: SubmitPracticeDto,
  ) {
    return this.practicesService.submitAnswer(req.user.userId, id, body.answer, body.duration || 0);
  }
}
