package controller

import (
	"server/common"
	user_service "server/modules/user/service"
	"server/modules/yx/dto"
	yx_service "server/modules/yx/service"
	"strconv"

	"github.com/gin-gonic/gin"
)

type UserScoreController struct {
	userScoreService   *user_service.UserScoreService
	yxUserScoreService *yx_service.YxUserScoreService
}

func NewUserScoreController() *UserScoreController {
	return &UserScoreController{
		yxUserScoreService: yx_service.NewYxUserScoreService(),
		userScoreService:   user_service.NewUserScoreService(),
	}
}

// RegisterRoutes 注册路由
func (ctrl *UserScoreController) RegisterRoutes(rg *gin.RouterGroup) {
	group := rg.Group("/user/score")
	{
		group.GET("/", ctrl.GetActive)
		group.GET("/:id", ctrl.GetByID)
		group.GET("/list", ctrl.List)
		group.POST("/save-score", ctrl.SaveUserScore)
	}
}

// SaveUserScore 保存用户成绩
// @Summary      保存用户成绩
// @Tags         用户分数
// @Param        request  body  dto.SaveScoreRequest  true  "成绩信息"
// @Success      200  {object}  common.Response
// @Router       /user/score/save-score [post]
func (ctrl *UserScoreController) SaveUserScore(c *gin.Context) {
	var req dto.SaveScoreRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		common.Error(c, 400, "参数错误")
		return
	}
	if err := req.Validate(); err != nil {
		common.Error(c, 400, "校验失败: "+err.Error())
		return
	}
	req.CreateBy = common.GetLoginUser(c).ID
	// 直接调用 Service，不处理具体业务逻辑
	result, err := ctrl.userScoreService.SaveUserScore(&req)
	if err != nil {
		common.Error(c, 500, "保存失败: "+err.Error())
		return
	}
	common.Success(c, result)
}

// GetActive 获取当前用户的激活分数
// @Summary      获取当前用户的激活分数
// @Tags         用户分数
// @Success      200  {object}  common.Response
// @Router       /user/score [get]
func (ctrl *UserScoreController) GetActive(c *gin.Context) {
	loginUserId := common.GetLoginUser(c).ID
	item, err := ctrl.userScoreService.GetActiveByID(loginUserId)
	if err != nil {
		common.Error(c, 404, "未找到激活成绩")
		return
	}
	common.Success(c, item)
}

// GetByID 获取指定 ID 的分数
// @Summary      获取指定 ID 的分数
// @Tags         用户分数
// @Param        id   path      string  true  "成绩ID"
// @Success      200  {object}  common.Response
// @Router       /user/score/{id} [get]
func (ctrl *UserScoreController) GetByID(c *gin.Context) {
	id := c.Param("id")
	item, err := ctrl.userScoreService.GetByID(id)
	if err != nil {
		common.Error(c, 404, "记录不存在")
		return
	}
	common.Success(c, item)
}

// List 获取当前用户的成绩列表
// @Summary      获取当前用户的成绩列表
// @Tags         用户分数
// @Param        page  query  int  false  "页码"  default(1)
// @Param        size  query  int  false  "每页数量"  default(10)
// @Success      200  {object}  common.Response
// @Router       /user/score/list [get]
func (ctrl *UserScoreController) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	loginUserId := common.GetLoginUser(c).ID
	items, total, err := ctrl.userScoreService.ListByUser(loginUserId, page, size)
	if err != nil {
		common.Error(c, 500, err.Error())
		return
	}
	common.Success(c, gin.H{
		"items": items,
		"total": total,
	})
}
