import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { LessonService } from './lesson.service';
import { CreateLessonDto } from './dto/create-lesson.dto';
import { FinishLessonDto } from './dto/finish-lesson.dto';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';

// 教师端授课控制器
@Controller('teacher/lessons')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('teacher')
export class LessonController {
  constructor(private readonly lessonService: LessonService) {}

  @Get()
  findAll(@Request() req: any, @Query() query: any) {
    return this.lessonService.findByTeacher(req.user.userId, query);
  }

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

  @Post()
  create(@Request() req: any, @Body() dto: CreateLessonDto) {
    return this.lessonService.create(req.user.userId, req.user.tenantId, dto);
  }

  @Post(':id/start')
  start(@Request() req: any, @Param('id') id: string) {
    return this.lessonService.start(+id, req.user.userId);
  }

  @Post(':id/finish')
  finish(@Request() req: any, @Param('id') id: string, @Body() dto: FinishLessonDto) {
    return this.lessonService.finish(+id, req.user.userId, dto);
  }

  @Post(':id/cancel')
  cancel(@Request() req: any, @Param('id') id: string) {
    return this.lessonService.cancel(+id, req.user.userId);
  }

  @Post(':id/students/:studentId/record')
  saveStudentRecord(
    @Request() req: any,
    @Param('id') id: string,
    @Param('studentId') studentId: string,
    @Body() data: any
  ) {
    return this.lessonService.saveStudentRecord(+id, req.user.userId, +studentId, data);
  }

  @Get(':id/student-records')
  getStudentRecords(@Request() req: any, @Param('id') id: string) {
    return this.lessonService.getStudentRecords(+id, req.user.userId);
  }

  @Post(':id/student-records/batch')
  batchSaveStudentRecords(
    @Request() req: any,
    @Param('id') id: string,
    @Body() data: { records: Array<{ studentId: number; focus?: number; participation?: number; interest?: number; understanding?: number; notes?: string }> }
  ) {
    return this.lessonService.batchSaveStudentRecords(+id, req.user.userId, data.records);
  }

  // ==================== 课程反馈 ====================

  @Post(':id/feedback')
  submitFeedback(
    @Request() req: any,
    @Param('id') id: string,
    @Body() data: any
  ) {
    return this.lessonService.submitFeedback(+id, req.user.userId, data);
  }

  @Get(':id/feedback')
  getFeedback(@Request() req: any, @Param('id') id: string) {
    return this.lessonService.getFeedback(+id, req.user.userId);
  }
}

// 教师端反馈控制器
@Controller('teacher/feedbacks')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('teacher')
export class TeacherFeedbackController {
  constructor(private readonly lessonService: LessonService) {}

  @Get()
  findAll(@Request() req: any, @Query() query: any) {
    return this.lessonService.getFeedbacksByTenant(req.user.tenantId, {
      ...query,
      teacherId: req.user.userId,
    });
  }

  @Get('stats')
  getStats(@Request() req: any) {
    return this.lessonService.getTeacherFeedbackStats(req.user.userId);
  }
}

// 学校端反馈控制器
@Controller('school/feedbacks')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('school')
export class SchoolFeedbackController {
  constructor(private readonly lessonService: LessonService) {}

  @Get()
  findAll(@Request() req: any, @Query() query: any) {
    return this.lessonService.getFeedbacksByTenant(req.user.tenantId, query);
  }

  @Get('stats')
  getStats(@Request() req: any) {
    return this.lessonService.getFeedbackStats(req.user.tenantId);
  }
}
