package tenant

import (
	"context"
	"testing"

	"github.com/tidbcloud/account/internel/svc/org/handler"

	"github.com/stretchr/testify/assert"
	"github.com/tidbcloud/account/commons/config"
	"github.com/tidbcloud/account/idl/pbgen/proto/central"
	"github.com/tidbcloud/account/internel/dao/entity"
)

const (
	role1 = entity.UserRole("owner")
	role2 = entity.UserRole("member")
	role3 = entity.UserRole("billing_admin")
	role4 = entity.UserRole("admin")
)

func TestIsValidUserRole(t *testing.T) {
	assert.Nil(t, IsValidUserRole(role1))
	assert.Nil(t, IsValidUserRole(role2))
	assert.Nil(t, IsValidUserRole(role3))
	assert.NotNil(t, IsValidUserRole(role4))
}

//func TestValidateInviteUserReq(t *testing.T) {
//	emailList1 := []string{"a@pingcap.com"}
//	emailList2 := []string{"a@pingcap.com", "b@pingcap.com"}
//	emailList3 := []string{"a", "a@pingcap.com"}
//	req1 := tenant.InviteUserReq{
//		OrgId:     1,
//		ProjectID: 0,
//		EmailList: emailList1,
//		Role:      role1,
//	}
//	req2 := tenant.InviteUserReq{
//		OrgId:     1,
//		ProjectID: 0,
//		EmailList: emailList2,
//		Role:      role1,
//	}
//	req3 := tenant.InviteUserReq{
//		OrgId:     1,
//		ProjectID: 0,
//		EmailList: emailList3,
//		Role:      role1,
//	}
//	req4 := tenant.InviteUserReq{
//		OrgId:     1,
//		ProjectID: 0,
//		EmailList: emailList1,
//		Role:      role4,
//	}
//
//	assert.Nil(t, validateInviteUserReq(req1))
//	assert.Nil(t, validateInviteUserReq(req2))
//	assert.NotNil(t, validateInviteUserReq(req3))
//	assert.NotNil(t, validateInviteUserReq(req4))
//}

func TestValidateRegisterWithThirdParty(t *testing.T) {
	ctx := context.Background()
	req := &central.RegistrationReq{
		FirstName:                 "test",
		LastName:                  "user",
		Company:                   "pingcap",
		Email:                     "test@pingcap.com",
		Job:                       "Engineer",
		PhoneNumber:               "123456789",
		Password:                  "test_password",
		Country:                   "CN",
		AcceptService:             true,
		RecaptchaResponse:         "recaptcha_response",
		ThirdPartyAccountProvider: handler.AWSAccountProvider,
		ThirdPartyAccountToken:    "abc",
	}
	assert.Nil(t, validateRegistrationReq(ctx, req, config.Auth0DatabaseConnection))

	req1 := &central.RegistrationReq{
		FirstName:                 "test",
		LastName:                  "user",
		Company:                   "pingcap",
		Email:                     "test@pingcap.com",
		Job:                       "Engineer",
		PhoneNumber:               "123456789",
		Password:                  "test_password",
		Country:                   "CN",
		AcceptService:             true,
		RecaptchaResponse:         "recaptcha_response",
		ThirdPartyAccountProvider: handler.AWSAccountProvider,
		ThirdPartyAccountToken:    "abc",
	}
	assert.Nil(t, validateRegistrationReq(ctx, req1, config.Auth0DatabaseConnection))
}

func TestValidateUpdateOrgReq(t *testing.T) {
	req1 := &central.UpdateOrgReq{
		OrgId:           1,
		Name:            "test",
		Timezone:        0,
		UserSetTimezone: "+08:00",
	}
	req2 := &central.UpdateOrgReq{
		OrgId:           1,
		Name:            "",
		Timezone:        0,
		UserSetTimezone: "+08:00",
	}
	req3 := &central.UpdateOrgReq{
		OrgId:           1,
		Name:            "test",
		Timezone:        15,
		UserSetTimezone: "+15:00",
	}

	assert.Nil(t, validateUpdateOrgReq(req1))
	assert.Nil(t, validateUpdateOrgReq(req2))
	assert.NotNil(t, validateUpdateOrgReq(req3))
}
