package admin

import (
	"strconv"

	"github.com/go-playground/validator/v10"
	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	agentRechargeSvc "github.com/break/junhong_cmp_fiber/internal/service/agent_recharge"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"
)

// AgentRechargeHandler 代理预充值 Handler
type AgentRechargeHandler struct {
	service   *agentRechargeSvc.Service
	validator *validator.Validate
}

// NewAgentRechargeHandler 创建代理预充值 Handler
func NewAgentRechargeHandler(service *agentRechargeSvc.Service, validator *validator.Validate) *AgentRechargeHandler {
	return &AgentRechargeHandler{service: service, validator: validator}
}

// Create 创建代理充值订单
// POST /api/admin/agent-recharges
func (h *AgentRechargeHandler) Create(c *fiber.Ctx) error {
	var req dto.CreateAgentRechargeRequest
	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)
	}

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

	return response.Success(c, result)
}

// List 查询代理充值订单列表
// GET /api/admin/agent-recharges
func (h *AgentRechargeHandler) List(c *fiber.Ctx) error {
	var req dto.AgentRechargeListRequest
	if err := c.QueryParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

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

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

// Get 查询代理充值订单详情
// GET /api/admin/agent-recharges/:id
func (h *AgentRechargeHandler) Get(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)
}

// Reject 驳回代理充值订单
// POST /api/admin/agent-recharges/:id/reject
func (h *AgentRechargeHandler) 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.AgentRechargeRejectRequest
	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.Reject(c.UserContext(), uint(id), req.RejectionReason); err != nil {
		return err
	}

	return response.Success(c, nil)
}

// OfflinePay 确认线下充值
// POST /api/admin/agent-recharges/:id/offline-pay
func (h *AgentRechargeHandler) OfflinePay(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.AgentOfflinePayRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

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

	return response.Success(c, result)
}
