import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards, Req } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { CoursesService } from './courses.service';

@ApiTags('课程')
@Controller('courses')
export class CoursesController {
  constructor(private coursesService: CoursesService) {}

  @Get()
  async findAll(@Query() query: { page?: number; pageSize?: number; categoryId?: number; isFree?: boolean }) {
    return this.coursesService.findAll(query);
  }

  @Get(':id')
  async findById(@Param('id') id: string) {
    return this.coursesService.findById(+id);
  }

  @Post()
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async create(@Body() body: {
    title: string; description?: string; cover?: string; categoryId?: number;
    price?: number; isFree?: boolean;
  }) {
    return this.coursesService.create(body);
  }

  @Put(':id')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async update(@Param('id') id: string, @Body() body: any) {
    return this.coursesService.update(+id, body);
  }

  @Delete(':id')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async remove(@Param('id') id: string) {
    return this.coursesService.remove(+id);
  }

  @Post(':courseId/lessons/:lessonId/progress')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async updateProgress(
    @Req() req: any,
    @Param('courseId') courseId: string,
    @Param('lessonId') lessonId: string,
    @Body() body: { completed?: boolean; progress?: number },
  ) {
    return this.coursesService.updateProgress(
      req.user.userId,
      +courseId,
      +lessonId,
      body.completed,
      body.progress,
    );
  }

  @Get('my-learning')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async getMyLearning(@Req() req: any, @Query('courseId') courseId?: string) {
    return this.coursesService.getLearningProgress(
      req.user.userId,
      courseId ? +courseId : undefined,
    );
  }
}
