package wechat

import (
	"context"
	"testing"

	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uber.org/zap"
)

func TestPaymentService_ParameterValidation(t *testing.T) {
	logger := zap.NewNop()
	mockSvc := &MockPaymentService{}

	t.Run("CreateJSAPIOrder_参数验证", func(t *testing.T) {
		mockSvc.CreateJSAPIOrderFn = func(ctx context.Context, orderNo, description, openID string, amount int) (*JSAPIPayResult, error) {
			if orderNo == "" || openID == "" || amount <= 0 {
				return nil, errors.New(errors.CodeInvalidParam, "订单号、OpenID 和金额不能为空")
			}
			return &JSAPIPayResult{PrepayID: "prepay_id_123"}, nil
		}

		_, err := mockSvc.CreateJSAPIOrder(context.Background(), "", "desc", "openid", 100)
		require.Error(t, err)

		_, err = mockSvc.CreateJSAPIOrder(context.Background(), "order_123", "desc", "", 100)
		require.Error(t, err)

		_, err = mockSvc.CreateJSAPIOrder(context.Background(), "order_123", "desc", "openid", 0)
		require.Error(t, err)

		result, err := mockSvc.CreateJSAPIOrder(context.Background(), "order_123", "desc", "openid", 100)
		require.NoError(t, err)
		assert.NotNil(t, result)
		assert.Equal(t, "prepay_id_123", result.PrepayID)
	})

	t.Run("CreateH5Order_参数验证", func(t *testing.T) {
		mockSvc.CreateH5OrderFn = func(ctx context.Context, orderNo, description string, amount int, sceneInfo *H5SceneInfo) (*H5PayResult, error) {
			if orderNo == "" || amount <= 0 {
				return nil, errors.New(errors.CodeInvalidParam, "订单号和金额不能为空")
			}
			return &H5PayResult{H5URL: "https://wx.tenpay.com/..."}, nil
		}

		_, err := mockSvc.CreateH5Order(context.Background(), "", "desc", 100, nil)
		require.Error(t, err)

		_, err = mockSvc.CreateH5Order(context.Background(), "order_123", "desc", 0, nil)
		require.Error(t, err)

		result, err := mockSvc.CreateH5Order(context.Background(), "order_123", "desc", 100, nil)
		require.NoError(t, err)
		assert.NotNil(t, result)
		assert.NotEmpty(t, result.H5URL)
	})

	t.Run("QueryOrder_参数验证", func(t *testing.T) {
		mockSvc.QueryOrderFn = func(ctx context.Context, orderNo string) (*OrderInfo, error) {
			if orderNo == "" {
				return nil, errors.New(errors.CodeInvalidParam, "订单号不能为空")
			}
			return &OrderInfo{OutTradeNo: orderNo}, nil
		}

		_, err := mockSvc.QueryOrder(context.Background(), "")
		require.Error(t, err)

		result, err := mockSvc.QueryOrder(context.Background(), "order_123")
		require.NoError(t, err)
		assert.NotNil(t, result)
		assert.Equal(t, "order_123", result.OutTradeNo)
	})

	t.Run("CloseOrder_参数验证", func(t *testing.T) {
		mockSvc.CloseOrderFn = func(ctx context.Context, orderNo string) error {
			if orderNo == "" {
				return errors.New(errors.CodeInvalidParam, "订单号不能为空")
			}
			return nil
		}

		err := mockSvc.CloseOrder(context.Background(), "")
		require.Error(t, err)

		err = mockSvc.CloseOrder(context.Background(), "order_123")
		require.NoError(t, err)
	})

	_ = logger
}
