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

// 超管端课程控制器
@Controller('admin/courses/:courseId/lessons')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
export class CourseLessonController {
  constructor(private readonly lessonService: CourseLessonService) {}

  @Get()
  async findAll(@Param('courseId', ParseIntPipe) courseId: number) {
    return this.lessonService.findAll(courseId);
  }

  @Get(':id')
  async findOne(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Param('id', ParseIntPipe) id: number,
  ) {
    return this.lessonService.findOne(id);
  }

  @Get('type/:lessonType')
  async findByType(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Param('lessonType') lessonType: string,
  ) {
    return this.lessonService.findByType(courseId, lessonType);
  }

  @Post()
  async create(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Body()
    body: {
      lessonType: string;
      name: string;
      description?: string;
      duration?: number;
      videoPath?: string;
      videoName?: string;
      pptPath?: string;
      pptName?: string;
      pdfPath?: string;
      pdfName?: string;
      objectives?: string;
      preparation?: string;
      extension?: string;
      reflection?: string;
      assessmentData?: string;
      useTemplate?: boolean;
    },
  ) {
    return this.lessonService.create(courseId, body);
  }

  @Put(':id')
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body()
    body: {
      name?: string;
      description?: string;
      duration?: number;
      videoPath?: string;
      videoName?: string;
      pptPath?: string;
      pptName?: string;
      pdfPath?: string;
      pdfName?: string;
      objectives?: string;
      preparation?: string;
      extension?: string;
      reflection?: string;
      assessmentData?: string;
      useTemplate?: boolean;
    },
  ) {
    return this.lessonService.update(id, body);
  }

  @Delete(':id')
  async remove(@Param('id', ParseIntPipe) id: number) {
    return this.lessonService.delete(id);
  }

  @Put('reorder')
  async reorder(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Body() body: { lessonIds: number[] },
  ) {
    return this.lessonService.reorder(courseId, body.lessonIds);
  }

  // ==================== 教学环节管理 ====================

  @Get(':lessonId/steps')
  async findSteps(@Param('lessonId', ParseIntPipe) lessonId: number) {
    return this.lessonService.findSteps(lessonId);
  }

  @Post(':lessonId/steps')
  async createStep(
    @Param('lessonId', ParseIntPipe) lessonId: number,
    @Body()
    body: {
      name: string;
      content?: string;
      duration?: number;
      objective?: string;
      resourceIds?: number[];
    },
  ) {
    return this.lessonService.createStep(lessonId, body);
  }

  @Put('steps/:stepId')
  async updateStep(
    @Param('stepId', ParseIntPipe) stepId: number,
    @Body()
    body: {
      name?: string;
      content?: string;
      duration?: number;
      objective?: string;
      resourceIds?: number[];
    },
  ) {
    return this.lessonService.updateStep(stepId, body);
  }

  @Delete('steps/:stepId')
  async removeStep(@Param('stepId', ParseIntPipe) stepId: number) {
    return this.lessonService.deleteStep(stepId);
  }

  @Put(':lessonId/steps/reorder')
  async reorderSteps(
    @Param('lessonId', ParseIntPipe) lessonId: number,
    @Body() body: { stepIds: number[] },
  ) {
    return this.lessonService.reorderSteps(lessonId, body.stepIds);
  }
}

// 教师端课程控制器
@Controller('teacher/courses/:courseId/lessons')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('teacher')
export class TeacherCourseLessonController {
  constructor(private readonly lessonService: CourseLessonService) {}

  @Get()
  async findAll(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Request() req: any,
  ) {
    return this.lessonService.findCourseLessonsForTeacher(courseId, req.user.tenantId);
  }

  @Get(':id')
  async findOne(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Param('id', ParseIntPipe) id: number,
    @Request() req: any,
  ) {
    // 先验证权限
    await this.lessonService.findCourseLessonsForTeacher(courseId, req.user.tenantId);
    return this.lessonService.findOne(id);
  }

  @Get('type/:lessonType')
  async findByType(
    @Param('courseId', ParseIntPipe) courseId: number,
    @Param('lessonType') lessonType: string,
    @Request() req: any,
  ) {
    // 先验证权限
    await this.lessonService.findCourseLessonsForTeacher(courseId, req.user.tenantId);
    return this.lessonService.findByType(courseId, lessonType);
  }
}
