package admin

import (
	"strconv"

	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	refundService "github.com/break/junhong_cmp_fiber/internal/service/refund"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"
)

// RefundHandler 退款管理处理器
type RefundHandler struct {
	service *refundService.Service
}

// NewRefundHandler 创建退款管理处理器
func NewRefundHandler(service *refundService.Service) *RefundHandler {
	return &RefundHandler{service: service}
}

// Create 创建退款申请
// POST /api/admin/refunds
func (h *RefundHandler) Create(c *fiber.Ctx) error {
	var req dto.CreateRefundRequest
	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)
}

// List 退款申请列表
// GET /api/admin/refunds
func (h *RefundHandler) List(c *fiber.Ctx) error {
	var req dto.RefundListRequest
	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)
}

// GetByID 退款申请详情
// GET /api/admin/refunds/:id
func (h *RefundHandler) GetByID(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的退款申请ID")
	}

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

	return response.Success(c, result)
}

// Approve 审批通过退款申请
// POST /api/admin/refunds/:id/approve
func (h *RefundHandler) Approve(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.ApproveRefundRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

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

	return response.Success(c, nil)
}

// Reject 审批拒绝退款申请
// POST /api/admin/refunds/:id/reject
func (h *RefundHandler) Reject(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.RejectRefundRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

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

	return response.Success(c, nil)
}

// Return 退回退款申请
// POST /api/admin/refunds/:id/return
func (h *RefundHandler) Return(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.ReturnRefundRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

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

	return response.Success(c, nil)
}

// Resubmit 重新提交退款申请
// POST /api/admin/refunds/:id/resubmit
func (h *RefundHandler) Resubmit(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.ResubmitRefundRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

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

	return response.Success(c, nil)
}
