package plant

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

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

type ExchangeApi struct{}

// ==================== 管理端 API ====================

// CreateItem 创建兑换商品
// @Tags 兑换中心-管理
// @Summary 创建兑换商品
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body request.ExchangeItemCreateReq true "创建兑换商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /exchange/item/create [post]
func (a *ExchangeApi) CreateItem(c *gin.Context) {
	var req request.ExchangeItemCreateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	if err := exchangeService.CreateItem(req); err != nil {
		global.Logger.Error("创建兑换商品失败", zap.Error(err))
		response.FailWithMsg("创建失败", c)
		return
	}
	response.OkWithMsg("创建成功", c)
}

// UpdateItem 更新兑换商品
// @Tags 兑换中心-管理
// @Summary 更新兑换商品
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body request.ExchangeItemUpdateReq true "更新兑换商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /exchange/item/update [post]
func (a *ExchangeApi) UpdateItem(c *gin.Context) {
	var req request.ExchangeItemUpdateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	if err := exchangeService.UpdateItem(req); err != nil {
		global.Logger.Error("更新兑换商品失败", zap.Error(err))
		response.FailWithMsg("更新失败", c)
		return
	}
	response.OkWithMsg("更新成功", c)
}

// DeleteItem 删除兑换商品
// @Tags 兑换中心-管理
// @Summary 删除兑换商品
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body request.ExchangeItemCreateReq true "删除兑换商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /exchange/item/delete [post]
func (a *ExchangeApi) DeleteItem(c *gin.Context) {
	var req struct {
		Id string `json:"id" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	if err := exchangeService.DeleteItem(req.Id); err != nil {
		global.Logger.Error("删除兑换商品失败", zap.Error(err))
		response.FailWithMsg("删除失败", c)
		return
	}
	response.OkWithMsg("删除成功", c)
}

// AdminItemList 管理端商品列表
// @Tags 兑换中心-管理
// @Summary 管理端商品列表
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body request.ExchangeItemListReq true "分页获取商品列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /exchange/item/list [post]
func (a *ExchangeApi) AdminItemList(c *gin.Context) {
	var req request.ExchangeItemListReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	list, total, err := exchangeService.AdminItemList(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)
}

// AdminOrderList 管理端订单列表
// @Tags 兑换中心-管理
// @Summary 管理端订单列表
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body request.ExchangeOrderListReq true "分页获取订单列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /exchange/order/list [post]
func (a *ExchangeApi) AdminOrderList(c *gin.Context) {
	var req request.ExchangeOrderListReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	list, total, err := exchangeService.AdminOrderList(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)
}

// UpdateOrderStatus 更新订单状态
// @Tags 兑换中心-管理
// @Summary 更新订单状态
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body request.ExchangeOrderUpdateReq true "更新订单状态"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /exchange/order/update [post]
func (a *ExchangeApi) UpdateOrderStatus(c *gin.Context) {
	var req request.ExchangeOrderUpdateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	if err := exchangeService.UpdateOrderStatus(req); err != nil {
		global.Logger.Error("更新订单状态失败", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithMsg("更新成功", c)
}

// ==================== 用户端 API ====================

// UserItemList 用户端商品列表
// @Tags 兑换中心-用户
// @Summary 用户端商品列表
// @Security BearerAuth
// @Produce application/json
// @Param current query int false "页码"
// @Param pageSize query int false "每页大小"
// @Param type query string false "商品类型"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /exchange/list [get]
func (a *ExchangeApi) UserItemList(c *gin.Context) {
	var req request.ExchangeItemListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	list, total, err := exchangeService.UserItemList(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)
}

// UserItemDetail 商品详情
// @Tags 兑换中心-用户
// @Summary 商品详情
// @Security BearerAuth
// @Produce application/json
// @Param id query string true "商品ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /exchange/detail [get]
func (a *ExchangeApi) UserItemDetail(c *gin.Context) {
	itemId := c.Query("id")
	if itemId == "" {
		response.FailWithMsg("参数错误", c)
		return
	}
	item, err := exchangeService.UserItemDetail(itemId)
	if err != nil {
		response.FailWithMsg("商品不存在", c)
		return
	}
	response.OkWithData(item, c)
}

// UserExchange 用户发起兑换
// @Tags 兑换中心-用户
// @Summary 发起兑换
// @Security BearerAuth
// @accept application/json
// @Produce application/json
// @Param data body request.ExchangeReq true "兑换请求"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"兑换成功"}"
// @Router /exchange/redeem [post]
func (a *ExchangeApi) UserExchange(c *gin.Context) {
	var req request.ExchangeReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	userId := auth.GetUserId(c)
	if err := exchangeService.UserExchange(req, userId); err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithMsg("兑换成功", c)
}

// UserOrderList 用户订单列表
// @Tags 兑换中心-用户
// @Summary 用户兑换记录
// @Security BearerAuth
// @Produce application/json
// @Param current query int false "页码"
// @Param pageSize query int false "每页大小"
// @Param status query int false "订单状态"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /exchange/orders [get]
func (a *ExchangeApi) UserOrderList(c *gin.Context) {
	var req request.ExchangeOrderListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.FailWithMsg("请求参数错误", c)
		return
	}
	userId := auth.GetUserId(c)
	list, total, err := exchangeService.UserOrderList(req, userId)
	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)
}
