package com.test.musichouduan.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.test.musichouduan.dto.Result;
import com.test.musichouduan.service.StatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 统计控制器
 */
@RestController
@RequestMapping("/api/stat")
@SaCheckRole("admin")
public class StatController {

    @Autowired
    private StatService statService;

    /**
     * 获取总体统计数据
     *
     * @return 响应结果
     */
    @GetMapping("/total")
    public Result<Map<String, Object>> getTotalStats() {
        Map<String, Object> stats = statService.getTotalStats();
        return Result.success(stats);
    }

    /**
     * 获取用户增长趋势
     *
     * @param days 天数
     * @return 响应结果
     */
    @GetMapping("/user/growth")
    public Result<Map<String, Object>> getUserGrowthTrend(@RequestParam(defaultValue = "7") Integer days) {
        Map<String, Object> stats = statService.getUserGrowthTrend(days);
        return Result.success(stats);
    }

    /**
     * 获取音乐类型分布
     *
     * @return 响应结果
     */
    @GetMapping("/music/type")
    public Result<Map<String, Object>> getMusicTypeDistribution() {
        Map<String, Object> stats = statService.getMusicTypeDistribution();
        return Result.success(stats);
    }

    /**
     * 获取播放量统计
     *
     * @param days 天数
     * @return 响应结果
     */
    @GetMapping("/play/count")
    public Result<Map<String, Object>> getPlayCountStats(@RequestParam(defaultValue = "7") Integer days) {
        Map<String, Object> stats = statService.getPlayCountStats(days);
        return Result.success(stats);
    }

    /**
     * 获取最新用户列表
     *
     * @param limit 限制数量
     * @return 响应结果
     */
    @GetMapping("/user/latest")
    public Result<Map<String, Object>> getLatestUsers(@RequestParam(defaultValue = "5") Integer limit) {
        Map<String, Object> stats = statService.getLatestUsers(limit);
        return Result.success(stats);
    }

    /**
     * 获取最新音乐列表
     *
     * @param limit 限制数量
     * @return 响应结果
     */
    @GetMapping("/music/latest")
    public Result<Map<String, Object>> getLatestMusics(@RequestParam(defaultValue = "5") Integer limit) {
        Map<String, Object> stats = statService.getLatestMusics(limit);
        return Result.success(stats);
    }
}
