package usecase

import (
	errs "backend/pkg/library/errors"
	"strconv"
	"testing"
	"time"

	"backend/pkg/member/domain/usecase"

	"github.com/stretchr/testify/assert"
)

func TestValidateGoogleTokenInfo(t *testing.T) {
	expectedClientID := "test-client-id"

	tests := []struct {
		name        string
		tokenInfo   usecase.GoogleTokenInfo
		expectedErr error
	}{
		{
			name: "Valid token",
			tokenInfo: usecase.GoogleTokenInfo{
				Exp:           strconv.FormatInt(time.Now().UTC().Add(10*time.Minute).Unix(), 10),
				Aud:           expectedClientID,
				EmailVerified: "true",
			},
			expectedErr: nil,
		},
		{
			name: "Token expired",
			tokenInfo: usecase.GoogleTokenInfo{
				Exp:           strconv.FormatInt(time.Now().UTC().Add(-10*time.Minute).Unix(), 10),
				Aud:           expectedClientID,
				EmailVerified: "true",
			},
			expectedErr: errs.SvcThirdPartyError("token is expired"),
		},
		{
			name: "Invalid audience",
			tokenInfo: usecase.GoogleTokenInfo{
				Exp:           strconv.FormatInt(time.Now().UTC().Add(10*time.Minute).Unix(), 10),
				Aud:           "invalid-client-id",
				EmailVerified: "true",
			},
			expectedErr: errs.SvcThirdPartyError("invalid audience"),
		},
		{
			name: "Email not verified",
			tokenInfo: usecase.GoogleTokenInfo{
				Exp:           strconv.FormatInt(time.Now().UTC().Add(10*time.Minute).Unix(), 10),
				Aud:           expectedClientID,
				EmailVerified: "false",
			},
			expectedErr: errs.SvcThirdPartyError("email is not verified"),
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			err := validateGoogleTokenInfo(test.tokenInfo, expectedClientID)

			if test.expectedErr != nil {
				assert.NotNil(t, err)
				assert.EqualError(t, err, test.expectedErr.Error())
			} else {
				assert.NoError(t, err)
			}
		})
	}
}
