import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Patch,
  Query,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { TokenData } from 'src/common/decorator/token-data.decorator';
import { ExamService } from 'src/assessment-evaluation/service/service-exam-manager/exam.service';
import { UpdateExamDto } from 'src/assessment-evaluation/dto/dtos-exam-manager/exam/update-exam.dto';
import { PagingExamDto } from 'src/assessment-evaluation/dto/dtos-exam-manager/exam/paging-exam.dto';
import { CreateExamDto } from 'src/assessment-evaluation/dto/dtos-exam-manager/exam/create-exam.dto';
import { StudentOnlineExamService } from 'src/assessment-evaluation/service/service-exam-manager/student-online-exam.service';
import { CreateStudentOnlineExamDto } from 'src/assessment-evaluation/dto/dtos-exam-manager/student-online-exam/create-student-online-exam.dto';

@ApiTags('考试管理 - 考试')
@ApiBearerAuth()
// @NoAuthToken()
@Controller('assessmentEvaluation/exam')
export class ExamController {
  constructor(
    private readonly ExamService: ExamService,
    private readonly studentOnlineExamService: StudentOnlineExamService,
  ) {}

  @Post()
  // @NoAuthToken()
  /* 创建考试的功能。 */
  async create(
    @Body() body: { exam: CreateExamDto; students: Array<any> },
    @TokenData() token: any,
  ) {
    body.exam.creator = body.exam.updater = token.userId;
    console.log(body);
    const newExam = await this.ExamService.create(body.exam);
    const studentsExamArr: CreateStudentOnlineExamDto[] = body.students.map(
      (item) => {
        return {
          userId: item,
          examId: newExam.id,
          mistakes: 0,
          examTimes: newExam.examTimes,
          creator: token.userId,
          updater: token.userId,
          delFlag: 0,
        };
      },
    );

    return await this.studentOnlineExamService.create(studentsExamArr);
  }

  @Get('item/:id')
  // @NoAuthToken()
  // 查找一个
  async findOne(@Param('id') id: string) {
    const exam = await this.ExamService.findOne(+id);
    const students = await this.studentOnlineExamService.findAllByExamId(+id);
    return { exam, students };
  }

  @Get('paging')
  // @NoAuthToken()
  // 分页查询
  async paging(@Query() pagingInfo: PagingExamDto, @TokenData() token: any) {
    return this.ExamService.paging(pagingInfo, token);
  }

  @Patch('updateIsAnonymous/:id')
  // 更新是否为匿名
  async updateIsAnonymous(
    @Param('id') id: string,
    @Body() body: { isAnonymous: number },
    // @TokenData() token: any,
  ) {
    return this.ExamService.updateIsAnonymous(+id, body.isAnonymous);
  }

  @Patch('item/:id')
  // 更新数据
  async update(
    @Param('id') id: string,
    @Body() body: { exam: UpdateExamDto; students: Array<any> },
    @TokenData() token: any,
  ) {
    await this.studentOnlineExamService.removeByExamId(+id);
    const studentsExamArr: CreateStudentOnlineExamDto[] = body.students.map(
      (item) => {
        return {
          userId: item,
          examId: +id,
          mistakes: 0,
          delFlag: 0,
          examTimes: body.exam.examTimes,
          creator: token.userId,
          updater: token.userId,
        };
      },
    );

    await this.studentOnlineExamService.create(studentsExamArr);
    return this.ExamService.update(+id, body.exam);
  }

  @Delete()
  // 批量删除考试
  async removeAll(@Body() ids: number[]) {
    try {
      for (const item of ids) {
        await this.ExamService.remove(+item);
        await this.studentOnlineExamService.removeByExamId(+item);
      }
    } catch (error) {
      return false;
    }
    return true;
  }
}
