package plant

import (
	"sundynix-go/global"
	"sundynix-go/model/commom/response"
	plantReq "sundynix-go/model/plant/request"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BadgeConfigApi struct{}

// GetBadgeTree 获取徽章配置树 (不分页)
// @Tags 徽章配置
// @Summary 获取徽章配置树形结构
// @Security BearerAuth
// @Produce application/json
// @Success 200 {string} string  "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /config/badge/tree [get]
func (a *BadgeConfigApi) GetBadgeTree(c *gin.Context) {
	// 不需要 bind query 了，因为没有参数
	tree, err := badgeConfigService.GetBadgeTree()
	if err != nil {
		global.Logger.Error("获取徽章树失败", zap.Error(err))
		response.FailWithMsg("获取数据失败", c)
		return
	}
	// 直接返回 data = tree
	response.OkWithData(tree, c)
}

// AddBadgeConfig 添加徽章配置
// @Tags 徽章配置
// @Summary 添加徽章配置
// @Security BearerAuth
// @accept application/json
// @Produce  application/json
// @Param data body plantReq.CreateBadge true "添加徽章配置"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
// @Router /config/badge/add [post]
func (a *BadgeConfigApi) AddBadgeConfig(c *gin.Context) {
	var req plantReq.CreateBadge
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("参数错误", c)
		return
	}
	err = badgeConfigService.AddBadgeConfig(req)
	if err != nil {
		global.Logger.Error("添加徽章失败", zap.Error(err))
		response.FailWithMsg("添加失败", c)
		return
	}
	response.OkWithMsg("添加成功", c)
}

// UpdateBadgeConfig 更新徽章配置
// @Tags 徽章配置
// @Summary 更新徽章配置
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body plantReq.UpdateBadge true "更新参数"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /config/badge/update [post]
func (a *BadgeConfigApi) UpdateBadgeConfig(c *gin.Context) {
	var req plantReq.UpdateBadge
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMsg("参数错误", c)
		return
	}
	err = badgeConfigService.UpdateBadgeConfig(req)
	if err != nil {
		global.Logger.Error("更新失败", zap.Error(err))
		response.FailWithMsg("更新失败", c)
		return
	}
	response.OkWithMsg("更新成功", c)
}

// FindBadgeConfig 根据ID获取徽章配置
// @Tags 徽章配置
// @Summary 根据ID获取徽章配置
// @Security BearerAuth
// @Produce application/json
// @Param id query string true "id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /config/badge/find [get]
func (a *BadgeConfigApi) FindBadgeConfig(c *gin.Context) {
	id := c.Query("id")
	badge, err := badgeConfigService.GetBadgeConfig(id)
	if err != nil {
		global.Logger.Error("查询失败", zap.Error(err))
		response.FailWithMsg("查询失败", c)
		return
	}
	response.OkWithData(badge, c)
}

// DeleteBadgeConfig 删除徽章配置
// @Tags 徽章配置
// @Summary 删除徽章配置
// @Security BearerAuth
// @Produce application/json
// @Param id query string true "id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /config/badge/delete [get]
func (a *BadgeConfigApi) DeleteBadgeConfig(c *gin.Context) {
	id := c.Query("id")
	err := badgeConfigService.DeleteBadgeConfig(id)
	if err != nil {
		global.Logger.Error("删除失败", zap.Error(err))
		response.FailWithMsg("删除失败", c)
		return
	}
	response.OkWithMsg("删除成功", c)
}
