package entities

import (
	"github.com/stretchr/testify/assert"
	"go-nkode/internal/models"
	"testing"
)

func TestCustomer(t *testing.T) {
	testNewCustomerAttributes(t)
	testCustomerValidKeyEntry(t)
	testCustomerIsValidNKode(t)
}

func testNewCustomerAttributes(t *testing.T) {
	_, nil := NewCustomerAttributes()
	assert.NoError(t, nil)
}

func testCustomerValidKeyEntry(t *testing.T) {
	kp := KeypadDimension{AttrsPerKey: 10, NumbOfKeys: 9}
	nkodePolicy := models.NewDefaultNKodePolicy()
	customer, err := NewCustomer(nkodePolicy)
	assert.NoError(t, err)
	mockSvgInterface := make(models.SvgIdInterface, kp.TotalAttrs())
	userInterface, err := NewUserInterface(&kp, mockSvgInterface)
	assert.NoError(t, err)
	userEmail := "testing@example.com"
	passcodeIdx := []int{0, 1, 2, 3}
	user, err := NewUser(*customer, userEmail, passcodeIdx, *userInterface, kp)
	assert.NoError(t, err)
	userLoginInterface, err := user.GetLoginInterface()
	assert.NoError(t, err)
	selectedKeys, err := SelectKeyByAttrIdx(userLoginInterface, passcodeIdx, kp)
	assert.NoError(t, err)
	validatedPasscode, err := ValidKeyEntry(*user, *customer, selectedKeys)
	assert.NoError(t, err)
	assert.Equal(t, len(validatedPasscode), len(passcodeIdx))
	for idx := range validatedPasscode {
		assert.Equal(t, validatedPasscode[idx], passcodeIdx[idx])
	}
}

func testCustomerIsValidNKode(t *testing.T) {
	kp := KeypadDimension{AttrsPerKey: 10, NumbOfKeys: 7}
	nkodePolicy := models.NewDefaultNKodePolicy()
	customer, err := NewCustomer(nkodePolicy)
	assert.NoError(t, err)
	mockSvgInterface := make(models.SvgIdInterface, kp.TotalAttrs())
	userInterface, err := NewUserInterface(&kp, mockSvgInterface)
	assert.NoError(t, err)
	userEmail := "testing123@example.com"
	passcodeIdx := []int{0, 1, 2, 3}
	user, err := NewUser(*customer, userEmail, passcodeIdx, *userInterface, kp)
	assert.NoError(t, err)
	err = customer.IsValidNKode(user.Kp, passcodeIdx)
	assert.NoError(t, err)
}
