import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Request,
  ParseIntPipe,
  Query,
} from '@nestjs/common';
import { HomeworksService } from './homeworks.service';
import { CreateHomeworkDto } from './dto/create-homework.dto';
import { UpdateHomeworkDto } from './dto/update-homework.dto';
import { QueryHomeworkDto } from './dto/query-homework.dto';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../../auth/decorators/require-permission.decorator';
import { CurrentTenantId } from '../../auth/decorators/current-tenant-id.decorator';

@Controller('homework/homeworks')
@UseGuards(JwtAuthGuard)
export class HomeworksController {
  constructor(private readonly homeworksService: HomeworksService) {}

  /**
   * 分页查询作业列表
   */
  @Get()
  @RequirePermission('homework:read')
  findAll(
    @Query() queryDto: QueryHomeworkDto,
    @CurrentTenantId() tenantId?: number,
  ) {
    return this.homeworksService.findAll(queryDto, tenantId);
  }

  /**
   * 获取我的作业列表（学生端）
   * 只返回已发布且对当前学生可见的作业，包含提交状态
   */
  @Get('my')
  @RequirePermission('homework-submission:create', 'homework-submission:read')
  findMyHomeworks(
    @Query() queryDto: QueryHomeworkDto,
    @CurrentTenantId() tenantId: number,
    @Request() req,
  ) {
    const userId = req?.user?.userId;
    return this.homeworksService.findMyHomeworks(queryDto, tenantId, userId);
  }

  /**
   * 创建作业
   */
  @Post()
  @RequirePermission('homework:create')
  create(
    @Body() createHomeworkDto: CreateHomeworkDto,
    @CurrentTenantId() tenantId: number,
    @Request() req?,
  ) {
    const creatorId = req?.user?.id;
    return this.homeworksService.create(createHomeworkDto, tenantId, creatorId);
  }

  /**
   * 根据ID查找作业
   */
  @Get(':id')
  @RequirePermission('homework:read', 'homework:student:read')
  findById(
    @Param('id', ParseIntPipe) id: number,
    @CurrentTenantId() tenantId?: number,
  ) {
    return this.homeworksService.findById(id, tenantId);
  }

  /**
   * 更新作业
   */
  @Patch(':id')
  @RequirePermission('homework:update')
  update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateHomeworkDto: UpdateHomeworkDto,
    @CurrentTenantId() tenantId?: number,
    @Request() req?,
  ) {
    const modifierId = req?.user?.id;
    return this.homeworksService.update(id, updateHomeworkDto, tenantId, modifierId);
  }

  /**
   * 发布作业
   */
  @Post(':id/publish')
  @RequirePermission('homework:update')
  publish(
    @Param('id', ParseIntPipe) id: number,
    @Body('publishScope') publishScope: number[],
    @CurrentTenantId() tenantId?: number,
    @Request() req?,
  ) {
    const modifierId = req?.user?.id;
    return this.homeworksService.publish(id, publishScope, tenantId, modifierId);
  }

  /**
   * 取消发布作业
   */
  @Post(':id/unpublish')
  @RequirePermission('homework:update')
  unpublish(
    @Param('id', ParseIntPipe) id: number,
    @CurrentTenantId() tenantId?: number,
    @Request() req?,
  ) {
    const modifierId = req?.user?.id;
    return this.homeworksService.unpublish(id, tenantId, modifierId);
  }

  /**
   * 删除作业（软删除）
   */
  @Delete(':id')
  @RequirePermission('homework:delete')
  remove(
    @Param('id', ParseIntPipe) id: number,
    @CurrentTenantId() tenantId?: number,
  ) {
    return this.homeworksService.remove(id, tenantId);
  }
}
