package admin

import (
	"strconv"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	exchangeService "github.com/break/junhong_cmp_fiber/internal/service/exchange"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"
	"github.com/go-playground/validator/v10"
	"github.com/gofiber/fiber/v2"
)

type ExchangeHandler struct {
	service   *exchangeService.Service
	validator *validator.Validate
}

func NewExchangeHandler(service *exchangeService.Service, validator *validator.Validate) *ExchangeHandler {
	return &ExchangeHandler{service: service, validator: validator}
}

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

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

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

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

func (h *ExchangeHandler) Get(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的换货单ID")
	}

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

func (h *ExchangeHandler) Ship(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的换货单ID")
	}

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

	data, err := h.service.Ship(c.UserContext(), uint(id), &req)
	if err != nil {
		return err
	}
	return response.Success(c, data)
}

func (h *ExchangeHandler) Complete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的换货单ID")
	}

	if err = h.service.Complete(c.UserContext(), uint(id)); err != nil {
		return err
	}
	return response.Success(c, nil)
}

func (h *ExchangeHandler) Cancel(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的换货单ID")
	}

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

	if err = h.service.Cancel(c.UserContext(), uint(id), &req); err != nil {
		return err
	}
	return response.Success(c, nil)
}

func (h *ExchangeHandler) Renew(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的换货单ID")
	}

	if err = h.service.Renew(c.UserContext(), uint(id)); err != nil {
		return err
	}
	return response.Success(c, nil)
}
