import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Patch,
  Query,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { NoAuthToken } from 'src/common/decorator/no-token.decorator';
import { TokenData } from 'src/common/decorator/token-data.decorator';
import { StudentOnlineExamService } from 'src/assessment-evaluation/service/service-exam-manager/student-online-exam.service';
import { TokenDataEntity } from 'src/common/entities/token-data.entity';
import { UpdateStudentOnlineExamDto } from 'src/assessment-evaluation/dto/dtos-exam-manager/student-online-exam/update-student-online-exam.dto';
import { CreateStudentOnlineExamDto } from 'src/assessment-evaluation/dto/dtos-exam-manager/student-online-exam/create-student-online-exam.dto';
import { PagingStudentOnlineExamDto } from 'src/assessment-evaluation/dto/dtos-exam-manager/student-online-exam/paging-student-online-exam.dto';
@ApiTags('考试管理 - 学员在线考试')
@ApiBearerAuth()
// @NoAuthToken()
@Controller('assessmentEvaluation/StudentOnlineExam')
export class StudentOnlineExamController {
  constructor(
    private readonly StudentOnlineExamService: StudentOnlineExamService,
  ) {}

  @Post()
  // @NoAuthToken()
  // 分发考试给学生
  async create(
    @Body() CreateStudentOnlineExamDto: CreateStudentOnlineExamDto,
    @TokenData() token: any,
  ) {
    CreateStudentOnlineExamDto.creator = CreateStudentOnlineExamDto.updater =
      token.userId;
    return this.StudentOnlineExamService.create(CreateStudentOnlineExamDto);
  }

  @Get('paging')
  // @NoAuthToken()
  // 分页查询考试历史
  async paging(
    @Query() pagingInfo: PagingStudentOnlineExamDto,
    @TokenData() token: TokenDataEntity,
  ) {
    console.log(token);
    return this.StudentOnlineExamService.paging(pagingInfo, token);
  }

  @Get('pagingGrade')
  // @NoAuthToken()
  @ApiOperation({ summary: '分页查询人工判卷的考试列表' })
  // 分页查询人工判卷的考试列表
  async pagingGrade(
    @Query() pagingInfo: PagingStudentOnlineExamDto,
    @TokenData() token: TokenDataEntity,
  ) {
    console.log(token);
    return this.StudentOnlineExamService.pagingGrade(pagingInfo, token);
  }
  @Get('pagingGradeDetails/:examId')
  @NoAuthToken()
  @ApiOperation({ summary: '分页查询需要人工判卷的学员考试列表' })
  // 分页查询需要人工判卷的学员考试列表
  async pagingGradeDetails(
    @Param('examId') examId: number,
    @Query() pagingInfo: PagingStudentOnlineExamDto,
  ) {
    return this.StudentOnlineExamService.pagingGradeDetails(
      +examId,
      pagingInfo,
    );
  }
  @Get('lastExamHistory/:onlineExamId')
  @NoAuthToken()
  // 获取考试最后一次的答卷
  async getLastExamHistory(@Param('onlineExamId') onlineExamId: number) {
    return this.StudentOnlineExamService.findLastExam(onlineExamId);
  }

  @Patch('item/:id')
  // 更新
  async update(
    @Param('id') id: string,
    @Body() UpdateStudentOnlineExamDto: UpdateStudentOnlineExamDto,
  ) {
    return this.StudentOnlineExamService.update(
      +id,
      UpdateStudentOnlineExamDto,
    );
  }
  // 删除
  @Delete('item/:id')
  async remove(@Param('id') id: string) {
    return this.StudentOnlineExamService.remove(+id);
  }
  // 批量删除
  @Delete()
  async removeAll(@Body() ids: number[]) {
    try {
      for (const item of ids) {
        await this.StudentOnlineExamService.remove(+item);
      }
    } catch (error) {
      return false;
    }
    return true;
  }
}
