import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  ParseIntPipe,
  UseGuards,
  Request,
} from '@nestjs/common';
import { SchoolCourseService } from './school-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('school/school-courses')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('school')
export class SchoolCourseController {
  constructor(private readonly schoolCourseService: SchoolCourseService) {}

  @Get()
  async findAll(@Request() req: any) {
    return this.schoolCourseService.findAll(req.user.tenantId);
  }

  @Get('source-courses')
  async getSourceCourses(@Request() req: any) {
    return this.schoolCourseService.getSourceCourses(req.user.tenantId);
  }

  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id: number, @Request() req: any) {
    return this.schoolCourseService.findOne(id, req.user.tenantId);
  }

  @Post()
  async create(
    @Request() req: any,
    @Body()
    body: {
      sourceCourseId: number;
      name: string;
      description?: string;
      changesSummary?: string;
    },
  ) {
    return this.schoolCourseService.create(
      req.user.tenantId,
      body.sourceCourseId,
      req.user.id,
      body,
    );
  }

  @Put(':id')
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Request() req: any,
    @Body()
    body: {
      name?: string;
      description?: string;
      changesSummary?: string;
      status?: string;
    },
  ) {
    return this.schoolCourseService.update(id, req.user.tenantId, body);
  }

  @Delete(':id')
  async remove(@Param('id', ParseIntPipe) id: number, @Request() req: any) {
    return this.schoolCourseService.delete(id, req.user.tenantId);
  }

  // 课程管理
  @Get(':id/lessons')
  async findLessons(
    @Param('id', ParseIntPipe) id: number,
    @Request() req: any,
  ) {
    return this.schoolCourseService.findLessons(id, req.user.tenantId);
  }

  @Put(':id/lessons/:lessonId')
  async updateLesson(
    @Param('id', ParseIntPipe) id: number,
    @Param('lessonId', ParseIntPipe) lessonId: number,
    @Request() req: any,
    @Body()
    body: {
      objectives?: string;
      preparation?: string;
      extension?: string;
      reflection?: string;
      changeNote?: string;
      stepsData?: string;
    },
  ) {
    return this.schoolCourseService.updateLesson(id, lessonId, req.user.tenantId, body);
  }

  // 预约管理
  @Get(':id/reservations')
  async findReservations(
    @Param('id', ParseIntPipe) id: number,
    @Request() req: any,
  ) {
    return this.schoolCourseService.findReservations(id, req.user.tenantId);
  }

  @Post(':id/reservations')
  async createReservation(
    @Param('id', ParseIntPipe) id: number,
    @Request() req: any,
    @Body()
    body: {
      teacherId: number;
      classId: number;
      scheduledDate: string;
      scheduledTime?: string;
      note?: string;
    },
  ) {
    return this.schoolCourseService.createReservation(id, req.user.tenantId, body);
  }

  @Post('reservations/:reservationId/cancel')
  async cancelReservation(
    @Param('reservationId', ParseIntPipe) reservationId: number,
    @Request() req: any,
  ) {
    return this.schoolCourseService.cancelReservation(reservationId, req.user.tenantId);
  }
}

// 教师端校本课程包控制器
@Controller('teacher/school-courses')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('teacher')
export class TeacherSchoolCourseController {
  constructor(private readonly schoolCourseService: SchoolCourseService) {}

  @Get()
  async findAll(@Request() req: any) {
    return this.schoolCourseService.findAll(req.user.tenantId, req.user.id);
  }

  @Get('source-courses')
  async getSourceCourses(@Request() req: any) {
    return this.schoolCourseService.getSourceCourses(req.user.tenantId);
  }

  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id: number, @Request() req: any) {
    return this.schoolCourseService.findOne(id, req.user.tenantId);
  }

  @Post()
  async create(
    @Request() req: any,
    @Body()
    body: {
      sourceCourseId: number;
      name: string;
      description?: string;
      changesSummary?: string;
    },
  ) {
    return this.schoolCourseService.create(
      req.user.tenantId,
      body.sourceCourseId,
      req.user.id,
      body,
    );
  }

  @Put(':id')
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Request() req: any,
    @Body()
    body: {
      name?: string;
      description?: string;
      changesSummary?: string;
      status?: string;
    },
  ) {
    return this.schoolCourseService.update(id, req.user.tenantId, body);
  }

  @Delete(':id')
  async remove(@Param('id', ParseIntPipe) id: number, @Request() req: any) {
    return this.schoolCourseService.delete(id, req.user.tenantId);
  }

  // 课程管理
  @Get(':id/lessons')
  async findLessons(
    @Param('id', ParseIntPipe) id: number,
    @Request() req: any,
  ) {
    return this.schoolCourseService.findLessons(id, req.user.tenantId);
  }

  @Put(':id/lessons/:lessonId')
  async updateLesson(
    @Param('id', ParseIntPipe) id: number,
    @Param('lessonId', ParseIntPipe) lessonId: number,
    @Request() req: any,
    @Body()
    body: {
      objectives?: string;
      preparation?: string;
      extension?: string;
      reflection?: string;
      changeNote?: string;
      stepsData?: string;
    },
  ) {
    return this.schoolCourseService.updateLesson(id, lessonId, req.user.tenantId, body);
  }
}
