import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { TaskService } from './task.service';
import {
  CreateTaskDto,
  UpdateTaskDto,
  UpdateCompletionDto,
  CreateTaskTemplateDto,
  UpdateTaskTemplateDto,
  CreateFromTemplateDto,
} from './dto/create-task.dto';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';
import { ScheduleNotificationService } from '../notification/schedule-notification.service';

// 学校端任务控制器
@Controller('school')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('school')
export class SchoolTaskController {
  constructor(
    private readonly taskService: TaskService,
    private readonly scheduleNotificationService: ScheduleNotificationService,
  ) {}

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

  @Get('tasks/stats')
  getStats(@Request() req: any) {
    return this.taskService.getStats(req.user.tenantId);
  }

  @Get('tasks/stats/by-type')
  getStatsByType(@Request() req: any) {
    return this.taskService.getStatsByType(req.user.tenantId);
  }

  @Get('tasks/stats/by-class')
  getStatsByClass(@Request() req: any) {
    return this.taskService.getStatsByClass(req.user.tenantId);
  }

  @Get('tasks/stats/monthly')
  getMonthlyStats(@Request() req: any, @Query('months') months?: string) {
    return this.taskService.getMonthlyStats(req.user.tenantId, months ? +months : 6);
  }

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

  @Get('tasks/:id/completions')
  getCompletions(@Request() req: any, @Param('id') id: string, @Query() query: any) {
    return this.taskService.getCompletions(req.user.tenantId, +id, query);
  }

  @Post('tasks')
  create(@Request() req: any, @Body() dto: CreateTaskDto) {
    return this.taskService.create(req.user.tenantId, req.user.userId, dto);
  }

  @Put('tasks/:id')
  update(@Request() req: any, @Param('id') id: string, @Body() dto: UpdateTaskDto) {
    return this.taskService.update(req.user.tenantId, +id, dto);
  }

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

  @Put('tasks/:taskId/completions/:studentId')
  updateCompletion(
    @Request() req: any,
    @Param('taskId') taskId: string,
    @Param('studentId') studentId: string,
    @Body() dto: UpdateCompletionDto,
  ) {
    return this.taskService.updateCompletion(req.user.tenantId, +taskId, +studentId, dto);
  }

  @Post('tasks/:id/remind')
  sendReminder(@Request() req: any, @Param('id') id: string) {
    return this.scheduleNotificationService.sendManualTaskReminder(req.user.tenantId, +id);
  }

  // ==================== 任务模板 ====================

  @Get('task-templates')
  findAllTemplates(@Request() req: any, @Query() query: any) {
    return this.taskService.findAllTemplates(req.user.tenantId, query);
  }

  @Get('task-templates/:id')
  findOneTemplate(@Request() req: any, @Param('id') id: string) {
    return this.taskService.findOneTemplate(req.user.tenantId, +id);
  }

  @Get('task-templates/default/:taskType')
  getDefaultTemplate(@Request() req: any, @Param('taskType') taskType: string) {
    return this.taskService.getDefaultTemplate(req.user.tenantId, taskType);
  }

  @Post('task-templates')
  createTemplate(@Request() req: any, @Body() dto: CreateTaskTemplateDto) {
    return this.taskService.createTemplate(req.user.tenantId, req.user.userId, dto);
  }

  @Put('task-templates/:id')
  updateTemplate(@Request() req: any, @Param('id') id: string, @Body() dto: UpdateTaskTemplateDto) {
    return this.taskService.updateTemplate(req.user.tenantId, +id, dto);
  }

  @Delete('task-templates/:id')
  deleteTemplate(@Request() req: any, @Param('id') id: string) {
    return this.taskService.deleteTemplate(req.user.tenantId, +id);
  }

  @Post('tasks/from-template')
  createFromTemplate(@Request() req: any, @Body() dto: CreateFromTemplateDto) {
    return this.taskService.createFromTemplate(
      req.user.tenantId,
      req.user.userId,
      dto.templateId,
      { targetIds: dto.targetIds, targetType: dto.targetType, startDate: dto.startDate },
    );
  }
}

// 教师端任务控制器
@Controller('teacher')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('teacher')
export class TeacherTaskController {
  constructor(
    private readonly taskService: TaskService,
    private readonly scheduleNotificationService: ScheduleNotificationService,
  ) {}

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

  @Get('tasks/upcoming')
  getUpcoming(@Request() req: any, @Query() query: any) {
    return this.taskService.getUpcoming(req.user.tenantId, query);
  }

  @Get('tasks/stats')
  getStats(@Request() req: any) {
    return this.taskService.getStats(req.user.tenantId);
  }

  @Get('tasks/stats/by-type')
  getStatsByType(@Request() req: any) {
    return this.taskService.getStatsByType(req.user.tenantId);
  }

  @Get('tasks/stats/by-class')
  getStatsByClass(@Request() req: any) {
    return this.taskService.getStatsByClass(req.user.tenantId);
  }

  @Get('tasks/stats/monthly')
  getMonthlyStats(@Request() req: any, @Query('months') months?: string) {
    return this.taskService.getMonthlyStats(req.user.tenantId, months ? +months : 6);
  }

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

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

  @Get('tasks/:id/completions')
  getCompletions(@Request() req: any, @Param('id') id: string, @Query() query: any) {
    return this.taskService.getCompletions(req.user.tenantId, +id, query);
  }

  @Post('tasks')
  create(@Request() req: any, @Body() dto: CreateTaskDto) {
    return this.taskService.create(req.user.tenantId, req.user.userId, dto);
  }

  @Put('tasks/:id')
  update(@Request() req: any, @Param('id') id: string, @Body() dto: UpdateTaskDto) {
    return this.taskService.update(req.user.tenantId, +id, dto);
  }

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

  @Post('tasks/:id/remind')
  sendReminder(@Request() req: any, @Param('id') id: string) {
    return this.scheduleNotificationService.sendManualTaskReminder(req.user.tenantId, +id);
  }

  @Put('tasks/:taskId/completions/:studentId')
  updateCompletion(
    @Request() req: any,
    @Param('taskId') taskId: string,
    @Param('studentId') studentId: string,
    @Body() dto: UpdateCompletionDto,
  ) {
    return this.taskService.updateCompletion(req.user.tenantId, +taskId, +studentId, dto);
  }

  // ==================== 任务模板（教师只读） ====================

  @Get('task-templates')
  findAllTemplates(@Request() req: any, @Query() query: any) {
    return this.taskService.findAllTemplates(req.user.tenantId, query);
  }

  @Get('task-templates/:id')
  findOneTemplate(@Request() req: any, @Param('id') id: string) {
    return this.taskService.findOneTemplate(req.user.tenantId, +id);
  }

  @Get('task-templates/default/:taskType')
  getDefaultTemplate(@Request() req: any, @Param('taskType') taskType: string) {
    return this.taskService.getDefaultTemplate(req.user.tenantId, taskType);
  }

  @Post('tasks/from-template')
  createFromTemplate(@Request() req: any, @Body() dto: CreateFromTemplateDto) {
    return this.taskService.createFromTemplate(
      req.user.tenantId,
      req.user.userId,
      dto.templateId,
      { targetIds: dto.targetIds, targetType: dto.targetType, startDate: dto.startDate },
    );
  }
}
