package com.moka.gdtauto.controller;

import com.moka.gdtauto.annotation.AuthUser;
import com.moka.gdtauto.common.Result;
import com.moka.gdtauto.entity.SysUser;
import com.moka.gdtauto.service.DashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Dashboard仪表盘控制器
 * 提供首页统计数据和系统状态信息
 * 
 * @author moka
 * @since 2026-02-06
 */
@Slf4j
@RestController
@RequestMapping("/api/dashboard")
@RequiredArgsConstructor
@Tag(name = "Dashboard仪表盘", description = "首页统计数据和系统状态API")
public class DashboardController {

    private final DashboardService dashboardService;

    /**
     * 获取首页统计概览数据
     * 包括视频、图片、广告组、转化等核心指标
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取统计概览", description = "获取视频、图片、广告组、转化等核心统计数据")
    public Result<Map<String, Object>> getStatisticsOverview() {
        try {
            Map<String, Object> statistics = dashboardService.getStatisticsOverview();
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取统计概览失败", e);
            return Result.error("获取统计概览失败: " + e.getMessage());
        }
    }

    /**
     * 获取视频相关统计数据
     */
    @GetMapping("/video-statistics")
    @Operation(summary = "获取视频统计", description = "获取视频上传数量、状态分布等统计数据")
    public Result<Map<String, Object>> getVideoStatistics() {
        try {
            Map<String, Object> statistics = dashboardService.getVideoStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取视频统计失败", e);
            return Result.error("获取视频统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取图片相关统计数据
     */
    @GetMapping("/image-statistics")
    @Operation(summary = "获取图片统计", description = "获取图片上传数量、状态分布等统计数据")
    public Result<Map<String, Object>> getImageStatistics() {
        try {
            Map<String, Object> statistics = dashboardService.getImageStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取图片统计失败", e);
            return Result.error("获取图片统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取广告组相关统计数据
     */
    @GetMapping("/adgroup-statistics")
    @Operation(summary = "获取广告组统计", description = "获取广告组数量、状态分布、营销目标等统计数据")
    public Result<Map<String, Object>> getAdgroupStatistics() {
        try {
            Map<String, Object> statistics = dashboardService.getAdgroupStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取广告组统计失败", e);
            return Result.error("获取广告组统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取转化相关统计数据
     */
    @GetMapping("/conversion-statistics")
    @Operation(summary = "获取转化统计", description = "获取转化归因数量、类型分布等统计数据")
    public Result<Map<String, Object>> getConversionStatistics() {
        try {
            Map<String, Object> statistics = dashboardService.getConversionStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取转化统计失败", e);
            return Result.error("获取转化统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取系统状态信息
     */
    @GetMapping("/system-status")
    @Operation(summary = "获取系统状态", description = "获取服务运行状态、版本信息、更新时间等")
    public Result<Map<String, Object>> getSystemStatus() {
        try {
            Map<String, Object> status = dashboardService.getSystemStatus();
            return Result.success(status);
        } catch (Exception e) {
            log.error("获取系统状态失败", e);
            return Result.error("获取系统状态失败: " + e.getMessage());
        }
    }

    /**
     * 获取最近活动记录
     */
    @GetMapping("/recent-activities")
    @Operation(summary = "获取最近活动", description = "获取最近的操作记录和系统事件")
    public Result<Map<String, Object>> getRecentActivities(
            @AuthUser SysUser user,
            @RequestParam(defaultValue = "5") Integer limit) {
        try {
            Map<String, Object> activities = dashboardService.getRecentActivities(user.getJobNumber(), limit);
            return Result.success(activities);
        } catch (Exception e) {
            log.error("获取最近活动失败", e);
            return Result.error("获取最近活动失败: " + e.getMessage());
        }
    }
}