package com.example.system.controller;

import com.example.system.model.Result;
import com.example.system.model.ResultCompetitionTable;
import com.example.system.service.CompetitionService;
import com.example.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/counts")
public class CountController {

    @Autowired
    private UserService userService;

    @Autowired
    private CompetitionService competitionService;

    /**
     * 获取系统统计数据
     * 包括：用户总数、教师人数、学生人数、比赛总数
     *
     * @return 统计数据
     */
    @GetMapping("/statistics")
    public Object getStatistics() {
        log.info("获取系统统计数据");
        Map<String, Object> statistics = new HashMap<>();

        // 获取用户总数
        Object totalUsers = userService.getCount();
        statistics.put("totalUsers", totalUsers);

        // 获取教师人数
        Object teacherCount = userService.getTeacherCount();
        statistics.put("teacherCount", teacherCount);

        // 获取学生人数
        Object studentCount = userService.getStudentCount();
        statistics.put("studentCount", studentCount);

        // 获取比赛总数
        List<ResultCompetitionTable> competitionList = competitionService.getAllCompetitions();
        int competitionCount = competitionList != null ? competitionList.size() : 0;
        statistics.put("competitionCount", competitionCount);

        return Result.success(statistics);
    }

    /**
     * 获取用户总数
     *
     * @return 用户总数
     */
    @GetMapping("/users/total")
    public Object getTotalUsers() {
        log.info("获取用户总数");
        Object count = userService.getCount();
        return Result.success(count);
    }

    /**
     * 获取教师人数
     *
     * @return 教师人数
     */
    @GetMapping("/users/teachers")
    public Object getTeacherCount() {
        log.info("获取教师人数");
        Object count = userService.getTeacherCount();
        return Result.success(count);
    }

    /**
     * 获取学生人数
     *
     * @return 学生人数
     */
    @GetMapping("/users/students")
    public Object getStudentCount() {
        log.info("获取学生人数");
        Object count = userService.getStudentCount();
        return Result.success(count);
    }

    /**
     * 获取比赛总数
     *
     * @return 比赛总数
     */
    @GetMapping("/competitions/total")
    public Object getCompetitionCount() {
        log.info("获取比赛总数");
        List<ResultCompetitionTable> competitionList = competitionService.getAllCompetitions();
        int count = competitionList != null ? competitionList.size() : 0;
        return Result.success(count);
    }
}