package cache_test

import (
	"encoding/json"
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/cache"
	"github.com/fiskerinc/cloud-services/pkg/redis"
	"github.com/fiskerinc/cloud-services/pkg/testhelper"
)

type mockRedisCacheVINs struct {
	redis.Connection
}

func (c *mockRedisCacheVINs) GetCache(id string, data interface{}, expire int) error {
	vins := []string{"TESTVIN123", "TESTVIN456"}

	dataBytes, err := json.Marshal(vins)
	if err != nil {
		return err
	}

	err = json.Unmarshal(dataBytes, data)
	if err != nil {
		return err
	}

	return nil
}

type mockRedisEmptyCacheVINs struct {
	redis.Connection
}

func (c *mockRedisEmptyCacheVINs) GetCache(id string, data interface{}, expire int) error {
	vins := []string{}

	dataBytes, err := json.Marshal(vins)
	if err != nil {
		return err
	}

	err = json.Unmarshal(dataBytes, data)
	if err != nil {
		return err
	}

	return nil
}

func (c *mockRedisEmptyCacheVINs) SetCache(id string, data interface{}, expire int) error {
	return nil
}

func TestRetrieveAndCacheVINs(t *testing.T) {
	setupRedisMock()
	setupDBMock()

	mockRedis = &mockRedisCacheVINs{}
	_, err := cache.RetrieveVINs(mockRedis, mockDB, "VALID_ID")
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestRetrieveAndCacheDriverIDs", nil, err)
	}

	mockRedis = &mockRedisEmptyCacheVINs{}
	_, err = cache.RetrieveVINs(mockRedis, mockDB, "VALID_ID")
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "TestRetrieveAndCacheDriverIDs", nil, err)
	}
}
