package h5

import (
	"strconv"

	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/model"
	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	orderService "github.com/break/junhong_cmp_fiber/internal/service/order"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/middleware"
	"github.com/break/junhong_cmp_fiber/pkg/response"
)

type OrderHandler struct {
	service *orderService.Service
}

func NewOrderHandler(service *orderService.Service) *OrderHandler {
	return &OrderHandler{service: service}
}

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

	if req.PaymentMethod != model.PaymentMethodWallet {
		return errors.New(errors.CodeInvalidParam, "H5端只支持钱包支付")
	}

	ctx := c.UserContext()
	userType := middleware.GetUserTypeFromContext(ctx)

	var buyerType string
	var buyerID uint

	switch userType {
	case constants.UserTypeAgent:
		buyerType = model.BuyerTypeAgent
		buyerID = middleware.GetShopIDFromContext(ctx)
	case constants.UserTypeEnterprise:
		return errors.New(errors.CodeForbidden, "企业账号不支持在线购买")
	case constants.UserTypePersonalCustomer:
		buyerType = model.BuyerTypePersonal
		buyerID = middleware.GetCustomerIDFromContext(ctx)
	default:
		return errors.New(errors.CodeForbidden, "不支持的用户类型")
	}

	order, err := h.service.Create(ctx, &req, buyerType, buyerID)
	if err != nil {
		return err
	}

	return response.Success(c, order)
}

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

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

	return response.Success(c, order)
}

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

	ctx := c.UserContext()
	userType := middleware.GetUserTypeFromContext(ctx)

	var buyerType string
	var buyerID uint

	switch userType {
	case constants.UserTypeAgent:
		buyerType = model.BuyerTypeAgent
		buyerID = middleware.GetShopIDFromContext(ctx)
	case constants.UserTypePersonalCustomer:
		buyerType = model.BuyerTypePersonal
		buyerID = middleware.GetCustomerIDFromContext(ctx)
	default:
		return errors.New(errors.CodeForbidden, "不支持的用户类型")
	}

	orders, err := h.service.List(ctx, &req, buyerType, buyerID)
	if err != nil {
		return err
	}

	return response.Success(c, orders)
}

func (h *OrderHandler) WalletPay(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的订单ID")
	}

	ctx := c.UserContext()
	userType := middleware.GetUserTypeFromContext(ctx)

	var buyerType string
	var buyerID uint

	switch userType {
	case constants.UserTypeAgent:
		buyerType = model.BuyerTypeAgent
		buyerID = middleware.GetShopIDFromContext(ctx)
	case constants.UserTypePersonalCustomer:
		buyerType = model.BuyerTypePersonal
		buyerID = middleware.GetCustomerIDFromContext(ctx)
	default:
		return errors.New(errors.CodeForbidden, "不支持的用户类型")
	}

	if err := h.service.WalletPay(ctx, uint(id), buyerType, buyerID); err != nil {
		return err
	}

	return response.Success(c, nil)
}

// WechatPayJSAPI 微信 JSAPI 支付
// POST /api/h5/orders/:id/wechat-pay/jsapi
func (h *OrderHandler) WechatPayJSAPI(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.WechatPayJSAPIRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	ctx := c.UserContext()
	userType := middleware.GetUserTypeFromContext(ctx)

	var buyerType string
	var buyerID uint

	switch userType {
	case constants.UserTypeAgent:
		buyerType = model.BuyerTypeAgent
		buyerID = middleware.GetShopIDFromContext(ctx)
	case constants.UserTypePersonalCustomer:
		buyerType = model.BuyerTypePersonal
		buyerID = middleware.GetCustomerIDFromContext(ctx)
	default:
		return errors.New(errors.CodeForbidden, "不支持的用户类型")
	}

	result, err := h.service.WechatPayJSAPI(ctx, uint(id), req.OpenID, buyerType, buyerID)
	if err != nil {
		return err
	}

	return response.Success(c, result)
}

// WechatPayH5 微信 H5 支付
// POST /api/h5/orders/:id/wechat-pay/h5
func (h *OrderHandler) WechatPayH5(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.WechatPayH5Request
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	ctx := c.UserContext()
	userType := middleware.GetUserTypeFromContext(ctx)

	var buyerType string
	var buyerID uint

	switch userType {
	case constants.UserTypeAgent:
		buyerType = model.BuyerTypeAgent
		buyerID = middleware.GetShopIDFromContext(ctx)
	case constants.UserTypePersonalCustomer:
		buyerType = model.BuyerTypePersonal
		buyerID = middleware.GetCustomerIDFromContext(ctx)
	default:
		return errors.New(errors.CodeForbidden, "不支持的用户类型")
	}

	result, err := h.service.WechatPayH5(ctx, uint(id), &req.SceneInfo, buyerType, buyerID)
	if err != nil {
		return err
	}

	return response.Success(c, result)
}
