package com.haha.admin.controller;

import com.haha.admin.annotation.RequirePermission;
import com.haha.common.vo.Result;
import com.haha.service.DashboardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 首页数据看板控制器
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    private final DashboardService dashboardService;

    /**
     * 获取概览数据（今日实时数据）
     */
    @RequirePermission("dashboard:read")
    @GetMapping("/overview")
    public Result<Map<String, Object>> getOverview() {
        try {
            Map<String, Object> data = dashboardService.getOverviewData();
            return Result.success("查询成功", data);
        } catch (Exception e) {
            log.error("获取概览数据失败: {}", e.getMessage(), e);
            return Result.error(500, "获取概览数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取统计数据（按时间维度）
     * @param type 时间类型：yesterday/week/month/days30
     */
    @RequirePermission("dashboard:read")
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics(@RequestParam(defaultValue = "week") String type) {
        try {
            Map<String, Object> data = dashboardService.getStatisticsData(type);
            return Result.success("查询成功", data);
        } catch (Exception e) {
            log.error("获取统计数据失败: {}", e.getMessage(), e);
            return Result.error(500, "获取统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取快捷入口数据
     */
    @RequirePermission("dashboard:read")
    @GetMapping("/quick-links")
    public Result<Map<String, Object>> getQuickLinks() {
        try {
            Map<String, Object> data = dashboardService.getQuickLinksData();
            return Result.success("查询成功", data);
        } catch (Exception e) {
            log.error("获取快捷入口数据失败: {}", e.getMessage(), e);
            return Result.error(500, "获取快捷入口数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取设备状态分布
     */
    @RequirePermission("dashboard:read")
    @GetMapping("/device-status")
    public Result<Object> getDeviceStatus() {
        try {
            Object data = dashboardService.getDeviceStatusData();
            return Result.success("查询成功", data);
        } catch (Exception e) {
            log.error("获取设备状态分布失败: {}", e.getMessage(), e);
            return Result.error(500, "获取设备状态分布失败: " + e.getMessage());
        }
    }
}
