package admin

import (
	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	commissionWithdrawalSettingService "github.com/break/junhong_cmp_fiber/internal/service/commission_withdrawal_setting"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"
)

type CommissionWithdrawalSettingHandler struct {
	service *commissionWithdrawalSettingService.Service
}

func NewCommissionWithdrawalSettingHandler(service *commissionWithdrawalSettingService.Service) *CommissionWithdrawalSettingHandler {
	return &CommissionWithdrawalSettingHandler{service: service}
}

func (h *CommissionWithdrawalSettingHandler) Create(c *fiber.Ctx) error {
	var req dto.CreateWithdrawalSettingReq
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	result, err := h.service.Create(c.UserContext(), &req)
	if err != nil {
		return err
	}

	return response.Success(c, result)
}

func (h *CommissionWithdrawalSettingHandler) List(c *fiber.Ctx) error {
	var req dto.WithdrawalSettingListReq
	if err := c.QueryParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	result, err := h.service.List(c.UserContext(), &req)
	if err != nil {
		return err
	}

	return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
}

func (h *CommissionWithdrawalSettingHandler) GetCurrent(c *fiber.Ctx) error {
	result, err := h.service.GetCurrent(c.UserContext())
	if err != nil {
		return err
	}

	return response.Success(c, result)
}
