package com.example.system.controller;

import com.example.system.model.RegistrationTable;
import com.example.system.model.Result;
import com.example.system.service.RegistrationService;
import com.example.system.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/registration")
public class RegistrationController {

    private final RegistrationService registrationService;

    @Autowired
    public RegistrationController(RegistrationService registrationService) {
        this.registrationService = registrationService;
    }

    /**
     * 根据参赛类型获取参赛人数
     * @return
     */
    @GetMapping("/getCountByCompetitionType")
    public Object getCountByCompetitionType(String competitionType) {
        log.info("根据参赛类型获取参赛人数 competitionType: {}", competitionType);
        return registrationService.getCountByCompetitionType(competitionType);
    }

    /**
     * 获取获奖人员的参赛记录
     * @return
     */
    @GetMapping("/getWinInfo")
    public Object getWinInfo() {
        return registrationService.getWinInfo();
    }

    /**
     * 获取参赛总人数
     * @return
     */
    @RequestMapping("/getCount")
    public Object getCount() {
        return registrationService.getCount();
    }

    /**
     * 获取成功报名并获奖的总人数
     * @return
     */
    public Object getWinCount() {
        return registrationService.getWinCount();
    }

    /**
     * 根据获奖等级获取参赛人数
     * @return
     */
    @RequestMapping("/getWinCountByLevel")
    public Object getCountBy(String level) {
        log.info("根据获奖等级获取参赛人数");
        return registrationService.getCountByLevel(level);
    }

    // 学生报名比赛
    @PostMapping("/enroll")
    public Object enrollInCompetition(RegistrationTable registration, HttpServletRequest request) {
        log.info("学生报名比赛 registration: {}", registration);
        String token =request.getHeader("Authorization");
        Claims claims = JWTUtil.checkToken(token);
        Integer userPrivileges = (Integer) claims.get("userPrivileges");
        if (userPrivileges != 2) {
            return Result.fail("只有学生可以报名");
        }
        return registrationService.enroll(registration);
    }

    // 教师审核学生的报名
    @PutMapping("/review")
    public Object reviewEnrollment(Long registrationId, String status, HttpServletRequest request) {
        log.info("教师审核学生的报名");
        String token =request.getHeader("Authorization");
        Claims claims = JWTUtil.checkToken(token);
        Integer userPrivileges = (Integer) claims.get("userPrivileges");
        if (userPrivileges == 2) {
            return Result.fail("只有教师可以审核报名");
        }
        return registrationService.updateStatus(registrationId, status);
    }

    //获取所有的报名信息
    @GetMapping("/getAll")
    public Object getAll(HttpServletRequest request) {
        log.info("获取所有的报名信息");
        String token =request.getHeader("Authorization");
        Claims claims = JWTUtil.checkToken(token);
        Integer userPrivileges = (Integer) claims.get("userPrivileges");
//        if (userPrivileges == 2) {
//            return Result.fail("只有教师可以");
//        }
        return registrationService.getAll();
    }
    //教师修改获奖等级和证书路径和附加信息
    @RequestMapping("/update")
    public Object updateRegistration(RegistrationTable table, HttpServletRequest request) {
        log.info("教师修改学生报名信息");
        String token =request.getHeader("Authorization");
        Claims claims = JWTUtil.checkToken(token);
        Integer userPrivileges = (Integer) claims.get("userPrivileges");
        if (userPrivileges == 2) {
            return Result.fail("只有教师可以修改");
        }
        return registrationService.update(table);
    }
}