import { Controller, Get, Param, Post, Query, UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { CurrentUser, AuthenticatedUser } from '../common/decorators/current-user.decorator';
import { ChaptersService } from './chapters.service';
import { QuestionsService } from '../questions/questions.service';
import { ProgressService } from '../progress/progress.service';

@UseGuards(JwtAuthGuard)
@Controller('chapters')
export class ChaptersController {
  constructor(
    private readonly chaptersService: ChaptersService,
    private readonly questionsService: QuestionsService,
    private readonly progressService: ProgressService,
  ) {}

  @Get()
  findAll(@CurrentUser() user: AuthenticatedUser, @Query('level_id') levelId?: string) {
    return this.chaptersService.findAll(user.userId, levelId);
  }

  @Get(':id')
  findOne(@Param('id') id: string, @CurrentUser() user: AuthenticatedUser) {
    return this.chaptersService.findOne(user.userId, id);
  }

  @Get(':id/questions')
  getQuestions(@Param('id') id: string, @CurrentUser() user: AuthenticatedUser) {
    return this.questionsService.getActiveQuestionsForChapter(user.userId, id);
  }

  @Post(':id/retry')
  retry(@Param('id') id: string, @CurrentUser() user: AuthenticatedUser) {
    return this.progressService.retryChapter(user.userId, id);
  }
}
