package admin

import (
	"strconv"

	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	shopPackageAllocationService "github.com/break/junhong_cmp_fiber/internal/service/shop_package_allocation"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"
)

type ShopPackageAllocationHandler struct {
	service *shopPackageAllocationService.Service
}

func NewShopPackageAllocationHandler(service *shopPackageAllocationService.Service) *ShopPackageAllocationHandler {
	return &ShopPackageAllocationHandler{service: service}
}

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

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

	return response.Success(c, allocation)
}

func (h *ShopPackageAllocationHandler) Get(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺套餐分配 ID")
	}

	allocation, err := h.service.Get(c.UserContext(), uint(id))
	if err != nil {
		return err
	}

	return response.Success(c, allocation)
}

func (h *ShopPackageAllocationHandler) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺套餐分配 ID")
	}

	var req dto.UpdateShopPackageAllocationRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	allocation, err := h.service.Update(c.UserContext(), uint(id), &req)
	if err != nil {
		return err
	}

	return response.Success(c, allocation)
}

func (h *ShopPackageAllocationHandler) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺套餐分配 ID")
	}

	if err := h.service.Delete(c.UserContext(), uint(id)); err != nil {
		return err
	}

	return response.Success(c, nil)
}

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

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

	return response.SuccessWithPagination(c, allocations, total, req.Page, req.PageSize)
}

func (h *ShopPackageAllocationHandler) UpdateStatus(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺套餐分配 ID")
	}

	var req dto.UpdateStatusRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	if err := h.service.UpdateStatus(c.UserContext(), uint(id), req.Status); err != nil {
		return err
	}

	return response.Success(c, nil)
}

// UpdateCostPrice 更新成本价
func (h *ShopPackageAllocationHandler) UpdateCostPrice(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺套餐分配 ID")
	}

	type UpdateCostPriceRequest struct {
		NewCostPrice int64  `json:"new_cost_price" validate:"required,min=0"`
		ChangeReason string `json:"change_reason" validate:"omitempty,max=255"`
	}

	var req UpdateCostPriceRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	result, err := h.service.UpdateCostPrice(c.UserContext(), uint(id), req.NewCostPrice, req.ChangeReason)
	if err != nil {
		return err
	}

	return response.Success(c, result)
}
