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 TestOfficialAccountService_ParameterValidation(t *testing.T) {
	logger := zap.NewNop()
	mockSvc := &MockOfficialAccountService{}

	t.Run("GetUserInfo_空授权码", func(t *testing.T) {
		mockSvc.GetUserInfoFn = func(ctx context.Context, code string) (string, string, error) {
			if code == "" {
				return "", "", errors.New(errors.CodeInvalidParam, "授权码不能为空")
			}
			return "openid_123", "unionid_123", nil
		}

		openID, unionID, err := mockSvc.GetUserInfo(context.Background(), "")
		require.Error(t, err)
		appErr, ok := err.(*errors.AppError)
		require.True(t, ok)
		assert.Equal(t, errors.CodeInvalidParam, appErr.Code)
		assert.Empty(t, openID)
		assert.Empty(t, unionID)
	})

	t.Run("GetUserInfo_成功", func(t *testing.T) {
		mockSvc.GetUserInfoFn = func(ctx context.Context, code string) (string, string, error) {
			return "openid_123", "unionid_123", nil
		}

		openID, unionID, err := mockSvc.GetUserInfo(context.Background(), "valid_code")
		require.NoError(t, err)
		assert.Equal(t, "openid_123", openID)
		assert.Equal(t, "unionid_123", unionID)
	})

	t.Run("GetUserInfoDetailed_空授权码", func(t *testing.T) {
		mockSvc.GetUserInfoDetailedFn = func(ctx context.Context, code string) (*UserInfo, error) {
			if code == "" {
				return nil, errors.New(errors.CodeInvalidParam, "授权码不能为空")
			}
			return &UserInfo{OpenID: "openid_123"}, nil
		}

		userInfo, err := mockSvc.GetUserInfoDetailed(context.Background(), "")
		require.Error(t, err)
		assert.Nil(t, userInfo)
	})

	t.Run("GetUserInfoByToken_空参数", func(t *testing.T) {
		mockSvc.GetUserInfoByTokenFn = func(ctx context.Context, accessToken, openID string) (*UserInfo, error) {
			if accessToken == "" || openID == "" {
				return nil, errors.New(errors.CodeInvalidParam, "AccessToken 和 OpenID 不能为空")
			}
			return &UserInfo{OpenID: openID}, nil
		}

		userInfo, err := mockSvc.GetUserInfoByToken(context.Background(), "", "openid_123")
		require.Error(t, err)
		assert.Nil(t, userInfo)

		userInfo, err = mockSvc.GetUserInfoByToken(context.Background(), "token_123", "")
		require.Error(t, err)
		assert.Nil(t, userInfo)
	})

	_ = logger
}
