package com.yupi.project.controller;

import com.yupi.project.annotation.AuthCheck;
import com.yupi.project.common.BaseResponse;
import com.yupi.project.common.ResultUtils;
import com.yupi.project.constant.UserConstant;
import com.yupi.project.model.vo.AdminDashboardStatsVO;
import com.yupi.project.service.ChargingRobotService;
import com.yupi.project.service.ChargingSessionService;
import com.yupi.project.service.ParkingSpotService;
import com.yupi.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 管理员统计信息接口
 */
@RestController
@RequestMapping("/admin/stats")
@Slf4j
@Api(tags = "管理员统计数据接口")
public class AdminStatsController {

    @Resource
    private UserService userService;

    @Resource
    private ChargingRobotService chargingRobotService;

    @Resource
    private ChargingSessionService chargingSessionService;

    @Resource
    private ParkingSpotService parkingSpotService;

    @ApiOperation("获取管理员仪表盘统计数据")
    @GetMapping("/summary")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<AdminDashboardStatsVO> getAdminDashboardStats() {
        long totalUsers = userService.count();
        long totalRobots = chargingRobotService.count(); 
        long onlineRobots = chargingRobotService.countOnlineRobots();
        long chargingRobots = chargingRobotService.countChargingRobots();
        long idleRobots = chargingRobotService.countIdleRobots();
        long activeSessions = chargingSessionService.countActiveSessions();
        BigDecimal totalRevenue = chargingSessionService.getTotalRevenue();
        long totalParkingSpots = parkingSpotService.count();
        long availableParkingSpots = parkingSpotService.countAvailableSpots();

        AdminDashboardStatsVO statsVO = new AdminDashboardStatsVO();
        statsVO.setTotalUsers(totalUsers);
        statsVO.setTotalRobots(totalRobots);
        statsVO.setOnlineRobots(onlineRobots);
        statsVO.setChargingRobots(chargingRobots);
        statsVO.setIdleRobots(idleRobots);
        statsVO.setActiveSessions(activeSessions);
        statsVO.setTotalRevenue(totalRevenue);
        statsVO.setTotalParkingSpots(totalParkingSpots);
        statsVO.setAvailableParkingSpots(availableParkingSpots);

        return ResultUtils.success(statsVO);
    }
} 