package pay

import (
	"context"
	"database/sql"
	"net/http"
	"qnc-server/app/main/api/internal/svc"
	"strings"
	"time"

	"github.com/pkg/errors"
	"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
	"github.com/zeromicro/go-zero/core/logx"
)

type WechatPayRefundCallbackLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewWechatPayRefundCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WechatPayRefundCallbackLogic {
	return &WechatPayRefundCallbackLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// handleQueryOrderRefund 处理查询订单退款
func (l *WechatPayRefundCallbackLogic) handleQueryOrderRefund(orderNo string, status refunddomestic.Status) error {
	order, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, orderNo)
	if err != nil {
		return errors.Wrapf(err, "查找查询订单信息失败: %s", orderNo)
	}

	if status == refunddomestic.STATUS_SUCCESS {
		order.Status = "refunded"
		order.RefundTime = sql.NullTime{
			Time:  time.Now(),
			Valid: true,
		}
	} else if status == refunddomestic.STATUS_ABNORMAL {
		return nil // 异常状态直接返回
	} else {
		return nil // 其他状态直接返回
	}

	if err := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); err != nil {
		return errors.Wrapf(err, "更新查询订单状态失败: %s", orderNo)
	}
	return nil
}

// handleAgentOrderRefund 处理代理会员订单退款
func (l *WechatPayRefundCallbackLogic) handleAgentOrderRefund(orderNo string, status refunddomestic.Status) error {
	order, err := l.svcCtx.AgentMembershipRechargeOrderModel.FindOneByOrderNo(l.ctx, orderNo)
	if err != nil {
		return errors.Wrapf(err, "查找代理会员订单信息失败: %s", orderNo)
	}

	if status == refunddomestic.STATUS_SUCCESS {
		order.Status = "refunded"
	} else if status == refunddomestic.STATUS_ABNORMAL {
		return nil // 异常状态直接返回
	} else {
		return nil // 其他状态直接返回
	}

	if err := l.svcCtx.AgentMembershipRechargeOrderModel.UpdateWithVersion(l.ctx, nil, order); err != nil {
		return errors.Wrapf(err, "更新代理会员订单状态失败: %s", orderNo)
	}
	return nil
}

// sendSuccessResponse 发送成功响应
func (l *WechatPayRefundCallbackLogic) sendSuccessResponse(w http.ResponseWriter) {
	w.WriteHeader(http.StatusOK)
	_, _ = w.Write([]byte("success"))
}

func (l *WechatPayRefundCallbackLogic) WechatPayRefundCallback(w http.ResponseWriter, r *http.Request) error {
	// 1. 处理微信退款通知
	notification, err := l.svcCtx.WechatPayService.HandleRefundNotification(l.ctx, r)
	if err != nil {
		logx.Errorf("微信退款回调处理失败: %v", err)
		l.sendSuccessResponse(w)
		return nil
	}

	orderNo := *notification.OutTradeNo
	var processErr error

	// 2. 根据订单号前缀处理不同类型的订单
	switch {
	case strings.HasPrefix(orderNo, "Q_"):
		processErr = l.handleQueryOrderRefund(orderNo, *notification.Status)
	case strings.HasPrefix(orderNo, "A_"):
		processErr = l.handleAgentOrderRefund(orderNo, *notification.Status)
	default:
		// 兼容旧订单，假设没有前缀的是查询订单
		processErr = l.handleQueryOrderRefund(orderNo, *notification.Status)
	}

	// 3. 处理错误并响应
	if processErr != nil {
		logx.Errorf("处理退款订单失败: %v", processErr)
	}

	// 无论处理是否成功，都返回成功响应给微信
	l.sendSuccessResponse(w)
	return nil
}
