package api

import (
	"github.com/gin-gonic/gin"
	"github.guxuan/haibei/internal/mods/customer/biz"
	"github.guxuan/haibei/internal/mods/customer/schema"
	"github.guxuan/haibei/pkg/util"
	"strconv"
)

// Defining the `Balance` api.
type Balance struct {
	BalanceBIZ *biz.Balance
}

// @Tags 积分模块
// @Security ApiKeyAuth
// @Summary Query balance list
// @Param current query int true "pagination index" default(1)
// @Param pageSize query int true "pagination size" default(10)
// @Success 200 {object} util.ResponseResult{data=[]schema.Balance}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/balances [get]
func (a *Balance) Query(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.BalanceQueryParam
	if err := util.ParseQuery(c, &params); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.BalanceBIZ.Query(ctx, params)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResPage(c, result.Data, result.PageResult)
}

// @Tags 积分模块
// @Security ApiKeyAuth
// @Summary Get balance record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult{data=schema.Balance}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/balances/{id} [get]
func (a *Balance) Get(c *gin.Context) {
	ctx := c.Request.Context()
	u64, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		util.ResError(c, err)
		return
	}
	item, err := a.BalanceBIZ.Get(ctx, uint(u64))
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, item)
}

// @Tags 积分模块
// @Security ApiKeyAuth
// @Summary Create balance record
// @Param body body schema.BalanceForm true "Request body"
// @Success 200 {object} util.ResponseResult{data=schema.Balance}
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/balances [post]
func (a *Balance) Create(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.BalanceForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.BalanceBIZ.Create(ctx, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, result)
}

// @Tags 积分模块
// @Security ApiKeyAuth
// @Summary Update balance record by ID
// @Param id path string true "unique id"
// @Param body body schema.BalanceForm true "Request body"
// @Success 200 {object} util.ResponseResult
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/balances/{id} [put]
func (a *Balance) Update(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.BalanceForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}
	u64, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.BalanceBIZ.Update(ctx, uint(u64), item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags 积分模块
// @Security ApiKeyAuth
// @Summary Delete balance record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/balances/{id} [delete]
func (a *Balance) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	u64, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.BalanceBIZ.Delete(ctx, uint(u64))
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
