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

@Controller('school')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('school')
export class GrowthController {
  constructor(private readonly growthService: GrowthService) {}

  @Get('growth-records')
  findAll(@Request() req: any, @Query() query: any) {
    return this.growthService.findAll(req.user.tenantId, query);
  }

  @Get('growth-records/:id')
  findOne(@Request() req: any, @Param('id') id: string) {
    return this.growthService.findOne(req.user.tenantId, +id);
  }

  @Post('growth-records')
  create(@Request() req: any, @Body() dto: CreateGrowthRecordDto) {
    return this.growthService.create(req.user.tenantId, req.user.userId, dto);
  }

  @Put('growth-records/:id')
  update(
    @Request() req: any,
    @Param('id') id: string,
    @Body() dto: UpdateGrowthRecordDto,
  ) {
    return this.growthService.update(req.user.tenantId, +id, dto);
  }

  @Delete('growth-records/:id')
  delete(@Request() req: any, @Param('id') id: string) {
    return this.growthService.delete(req.user.tenantId, +id);
  }

  @Get('students/:studentId/growth-records')
  findByStudent(
    @Request() req: any,
    @Param('studentId') studentId: string,
    @Query() query: any,
  ) {
    return this.growthService.findByStudent(req.user.tenantId, +studentId, query);
  }

  @Get('classes/:classId/growth-records')
  findByClass(
    @Request() req: any,
    @Param('classId') classId: string,
    @Query() query: any,
  ) {
    return this.growthService.findByClass(req.user.tenantId, +classId, query);
  }
}

// 教师端的成长档案控制器
@Controller('teacher')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('teacher')
export class TeacherGrowthController {
  constructor(private readonly growthService: GrowthService) {}

  @Get('growth-records')
  findAll(@Request() req: any, @Query() query: any) {
    return this.growthService.findAllForTeacher(req.user.tenantId, req.user.userId, query);
  }

  @Get('growth-records/:id')
  findOne(@Request() req: any, @Param('id') id: string) {
    return this.growthService.findOneForTeacher(req.user.tenantId, req.user.userId, +id);
  }

  @Post('growth-records')
  create(@Request() req: any, @Body() dto: CreateGrowthRecordDto) {
    return this.growthService.createForTeacher(req.user.tenantId, req.user.userId, dto);
  }

  @Put('growth-records/:id')
  update(
    @Request() req: any,
    @Param('id') id: string,
    @Body() dto: UpdateGrowthRecordDto,
  ) {
    return this.growthService.updateForTeacher(req.user.tenantId, req.user.userId, +id, dto);
  }

  @Delete('growth-records/:id')
  delete(@Request() req: any, @Param('id') id: string) {
    return this.growthService.deleteForTeacher(req.user.tenantId, req.user.userId, +id);
  }

  @Get('classes/:classId/growth-records')
  findByClass(
    @Request() req: any,
    @Param('classId') classId: string,
    @Query() query: any,
  ) {
    return this.growthService.findByClass(req.user.tenantId, +classId, query);
  }
}
