import { Controller, Get, Post, Put, Delete, Param, Query, Body, UseGuards, Request } from '@nestjs/common';
import { TeacherCourseService } from './teacher-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('teacher')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('teacher')
export class TeacherCourseController {
  constructor(private readonly teacherCourseService: TeacherCourseService) {}

  // ==================== 首页仪表板 ====================

  @Get('dashboard')
  getDashboard(@Request() req: any) {
    return this.teacherCourseService.getDashboard(req.user.userId, req.user.tenantId);
  }

  @Get('dashboard/today')
  getTodayLessons(@Request() req: any) {
    return this.teacherCourseService.getTodayLessons(req.user.userId, req.user.tenantId);
  }

  @Get('dashboard/recommend')
  getRecommendedCourses(@Request() req: any) {
    return this.teacherCourseService.getRecommendedCourses(req.user.tenantId);
  }

  @Get('dashboard/weekly')
  getWeeklyStats(@Request() req: any) {
    return this.teacherCourseService.getWeeklyStats(req.user.userId);
  }

  @Get('dashboard/lesson-trend')
  getLessonTrend(@Request() req: any, @Query('months') months?: string) {
    return this.teacherCourseService.getTeacherLessonTrend(
      req.user.userId,
      months ? parseInt(months, 10) : 6,
    );
  }

  @Get('dashboard/course-usage')
  getCourseUsage(@Request() req: any) {
    return this.teacherCourseService.getTeacherCourseUsage(req.user.userId);
  }

  // ==================== 课程管理 ====================

  @Get('courses')
  findAll(@Request() req: any, @Query() query: any) {
    return this.teacherCourseService.findAll(req.user.userId, req.user.tenantId, query);
  }

  @Get('courses/classes')
  getClasses(@Request() req: any) {
    return this.teacherCourseService.getTeacherClasses(req.user.userId);
  }

  @Get('courses/:id')
  findOne(@Request() req: any, @Param('id') id: string) {
    return this.teacherCourseService.findOne(+id, req.user.userId, req.user.tenantId);
  }

  // ==================== 班级学生管理 ====================

  @Get('students')
  getAllStudents(@Request() req: any, @Query() query: any) {
    return this.teacherCourseService.getAllTeacherStudents(req.user.userId, query);
  }

  @Get('classes/:id/students')
  getClassStudents(
    @Request() req: any,
    @Param('id') id: string,
    @Query() query: any,
  ) {
    return this.teacherCourseService.getClassStudents(req.user.userId, +id, query);
  }

  @Get('classes/:id/teachers')
  getClassTeachers(
    @Request() req: any,
    @Param('id') id: string,
  ) {
    return this.teacherCourseService.getClassTeachers(req.user.userId, +id);
  }

  // ==================== 排课管理 ====================

  @Get('schedules')
  getTeacherSchedules(@Request() req: any, @Query() query: any) {
    return this.teacherCourseService.getTeacherSchedules(req.user.userId, query);
  }

  @Get('schedules/timetable')
  getTeacherTimetable(
    @Request() req: any,
    @Query('startDate') startDate: string,
    @Query('endDate') endDate: string,
  ) {
    return this.teacherCourseService.getTeacherTimetable(req.user.userId, startDate, endDate);
  }

  @Get('schedules/today')
  getTodaySchedules(@Request() req: any) {
    return this.teacherCourseService.getTodaySchedules(req.user.userId);
  }

  @Post('schedules')
  createTeacherSchedule(@Request() req: any, @Body() dto: any) {
    return this.teacherCourseService.createTeacherSchedule(req.user.userId, req.user.tenantId, dto);
  }

  @Put('schedules/:id')
  updateTeacherSchedule(
    @Request() req: any,
    @Param('id') id: string,
    @Body() dto: any,
  ) {
    return this.teacherCourseService.updateTeacherSchedule(req.user.userId, +id, dto);
  }

  @Delete('schedules/:id')
  cancelTeacherSchedule(@Request() req: any, @Param('id') id: string) {
    return this.teacherCourseService.cancelTeacherSchedule(req.user.userId, +id);
  }
}
