import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { CourseService } from './course.service';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';

@Controller('courses')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
export class CourseController {
  constructor(private readonly courseService: CourseService) {}

  @Get()
  findAll(@Query() query: any) {
    return this.courseService.findAll(query);
  }

  @Get('review')
  getReviewList(@Query() query: any) {
    return this.courseService.getReviewList(query);
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.courseService.findOne(+id);
  }

  @Get(':id/stats')
  getStats(@Param('id') id: string) {
    return this.courseService.getStats(+id);
  }

  @Get(':id/validate')
  validate(@Param('id') id: string) {
    return this.courseService.validate(+id);
  }

  @Get(':id/versions')
  getVersionHistory(@Param('id') id: string) {
    return this.courseService.getVersionHistory(+id);
  }

  @Post()
  create(@Body() createCourseDto: any, @Request() req: any) {
    return this.courseService.create({
      ...createCourseDto,
      createdBy: req.user?.userId,
    });
  }

  @Put(':id')
  update(@Param('id') id: string, @Body() updateCourseDto: any) {
    return this.courseService.update(+id, updateCourseDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.courseService.remove(+id);
  }

  /**
   * 提交审核
   * POST /api/v1/courses/:id/submit
   */
  @Post(':id/submit')
  submit(@Param('id') id: string, @Body() body: { copyrightConfirmed: boolean }, @Request() req: any) {
    const userId = req.user?.userId || 0;
    return this.courseService.submit(+id, userId, body.copyrightConfirmed);
  }

  /**
   * 撤销审核申请
   * POST /api/v1/courses/:id/withdraw
   */
  @Post(':id/withdraw')
  withdraw(@Param('id') id: string, @Request() req: any) {
    const userId = req.user?.userId || 0;
    return this.courseService.withdraw(+id, userId);
  }

  /**
   * 审核通过
   * POST /api/v1/courses/:id/approve
   */
  @Post(':id/approve')
  approve(
    @Param('id') id: string,
    @Body() body: { checklist?: any; comment?: string },
    @Request() req: any,
  ) {
    const reviewerId = req.user?.userId || 0;
    return this.courseService.approve(+id, reviewerId, body);
  }

  /**
   * 审核驳回
   * POST /api/v1/courses/:id/reject
   */
  @Post(':id/reject')
  reject(
    @Param('id') id: string,
    @Body() body: { checklist?: any; comment: string },
    @Request() req: any,
  ) {
    const reviewerId = req.user?.userId || 0;
    return this.courseService.reject(+id, reviewerId, body);
  }

  /**
   * 直接发布（超级管理员专用）
   * POST /api/v1/courses/:id/direct-publish
   */
  @Post(':id/direct-publish')
  @Roles('admin')
  directPublish(
    @Param('id') id: string,
    @Body() body: { skipValidation?: boolean },
    @Request() req: any,
  ) {
    const userId = req.user?.userId || 0;
    return this.courseService.directPublish(+id, userId, body.skipValidation);
  }

  /**
   * 发布课程（兼容旧API）
   * POST /api/v1/courses/:id/publish
   */
  @Post(':id/publish')
  publish(@Param('id') id: string) {
    return this.courseService.publish(+id);
  }

  /**
   * 下架课程
   * POST /api/v1/courses/:id/unpublish
   */
  @Post(':id/unpublish')
  unpublish(@Param('id') id: string) {
    return this.courseService.unpublish(+id);
  }

  /**
   * 重新发布已下架的课程
   * POST /api/v1/courses/:id/republish
   */
  @Post(':id/republish')
  republish(@Param('id') id: string) {
    return this.courseService.republish(+id);
  }
}
