package handler

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"github.com/user-management-system/internal/service"
)

// StatsHandler handles statistics requests
type StatsHandler struct {
	statsService *service.StatsService
}

// NewStatsHandler creates a new StatsHandler
func NewStatsHandler(statsService *service.StatsService) *StatsHandler {
	return &StatsHandler{statsService: statsService}
}

// GetDashboard 获取仪表盘统计
// @Summary 获取仪表盘统计
// @Description 获取系统仪表盘统计数据（仅管理员）
// @Tags 统计
// @Produce json
// @Security BearerAuth
// @Success 200 {object} Response{data=service.DashboardStats} "仪表盘数据"
// @Failure 403 {object} Response "无权限"
// @Failure 500 {object} Response "服务器错误"
// @Router /api/v1/admin/stats/dashboard [get]
func (h *StatsHandler) GetDashboard(c *gin.Context) {
	stats, err := h.statsService.GetDashboardStats(c.Request.Context())
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取仪表盘数据失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": stats})
}

// GetUserStats 获取用户统计
// @Summary 获取用户统计
// @Description 获取用户统计数据（仅管理员）
// @Tags 统计
// @Produce json
// @Security BearerAuth
// @Success 200 {object} Response{data=service.UserStats} "用户统计数据"
// @Failure 403 {object} Response "无权限"
// @Failure 500 {object} Response "服务器错误"
// @Router /api/v1/admin/stats/users [get]
func (h *StatsHandler) GetUserStats(c *gin.Context) {
	stats, err := h.statsService.GetUserStats(c.Request.Context())
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取用户统计失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": stats})
}
