package plant

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

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

type BannerApi struct{}

// Create 创建Banner
// @Tags 轮播图管理
// @Summary 创建Banner
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body plant.Banner true "创建Banner"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /plantBanner/create [post]
func (a *BannerApi) Create(c *gin.Context) {
	var req plant.Banner
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("参数错误", c)
		return
	}
	if err := bannerService.Create(req); err != nil {
		global.Logger.Error("创建失败", zap.Error(err))
		response.FailWithMsg("创建失败", c)
		return
	}
	response.OkWithMsg("创建成功", c)
}

// Delete 删除Banner
// @Tags 轮播图管理
// @Summary 删除Banner
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body object true "删除Banner"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /plantBanner/delete [post]
func (a *BannerApi) Delete(c *gin.Context) {
	var req struct {
		Id string `json:"id"`
	}
	if err := c.ShouldBindJSON(&req); err != nil || req.Id == "" {
		response.FailWithMsg("参数错误", c)
		return
	}
	if err := bannerService.Delete(req.Id); err != nil {
		global.Logger.Error("删除失败", zap.Error(err))
		response.FailWithMsg("删除失败", c)
		return
	}
	response.OkWithMsg("删除成功", c)
}

// Update 更新Banner
// @Tags 轮播图管理
// @Summary 更新Banner
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body plant.Banner true "更新Banner"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /plantBanner/update [put]
func (a *BannerApi) Update(c *gin.Context) {
	var req plant.Banner
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("参数错误", c)
		return
	}
	if err := bannerService.Update(req); err != nil {
		global.Logger.Error("更新失败", zap.Error(err))
		response.FailWithMsg("更新失败", c)
		return
	}
	response.OkWithMsg("更新成功", c)
}

// GetList 分页获取Banner
// @Tags 轮播图管理
// @Summary 分页获取Banner
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body request.BannerPageReq true "分页获取Banner"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /plantBanner/list [post]
func (a *BannerApi) GetList(c *gin.Context) {
	var req request.BannerPageReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("参数错误", c)
		return
	}
	list, total, err := bannerService.GetList(req)
	if err != nil {
		global.Logger.Error("获取失败", zap.Error(err))
		response.FailWithMsg("获取失败", c)
		return
	}
	response.OkWithData(response.PageResult{
		List:     list,
		Total:    total,
		Page:     req.Current,
		PageSize: req.PageSize,
	}, c)
}

// GetActiveList 客户端获取启用的Banner
// @Tags 轮播图管理
// @Summary 客户端获取启用的Banner
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /plantBanner/activeList [get]
func (a *BannerApi) GetActiveList(c *gin.Context) {
	list, err := bannerService.GetActiveList()
	if err != nil {
		global.Logger.Error("获取失败", zap.Error(err))
		response.FailWithMsg("获取失败", c)
		return
	}
	response.OkWithData(response.ListResult{
		List: list,
	}, c)
}
