package queries_test

import (
	"encoding/json"
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/db"
	"github.com/fiskerinc/cloud-services/pkg/db/queries"
	"github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestSelectAllPrivateKeysByVIN(t *testing.T) {
	t.Skip()
	client := db.DBClient{}
	client.GetConn().AddQueryHook(db.SQLLogger{})
	q := queries.EccKeys{}
	q.SetClient(&client)
	result, err := q.SelectAllPrivateKeysByVIN("3FAFP13P71R199432")
	if err != nil {
		t.Error(err)
		return
	}
	if len(result) == 0 {
		t.Errorf(testhelper.TestErrorTemplate, "Existing VIN", "more than 0 keys", 0)
	}

	result, err = q.SelectAllPrivateKeysByVIN("3FAFP13P71R19943X")
	if err != nil {
		t.Error(err)
		return
	}
	if len(result) == 0 {
		t.Errorf(testhelper.TestErrorTemplate, "Non-existing VIN", "more than 0 keys", 0)
	}
}

func TestSelectAllPrivateKeysByCarUpdateID(t *testing.T) {
	t.Skip()
	client := db.DBClient{}
	client.GetConn().AddQueryHook(db.SQLLogger{})
	q := queries.EccKeys{}
	q.SetClient(&client)
	result, err := q.SelectAllPrivateKeysByCarUpdateID(3497)
	if err != nil {
		t.Error(err)
		return
	}
	if len(result) == 0 {
		t.Errorf(testhelper.TestErrorTemplate, "Existing car update id", "more than 0 keys", 0)
	}

	result, err = q.SelectAllPrivateKeysByCarUpdateID(0)
	if err != nil {
		t.Error(err)
		return
	}
	if len(result) == 0 {
		t.Errorf(testhelper.TestErrorTemplate, "non-existing car update id", "more than 0 keys", 0)
	}
}

// Use for getting keys from db
func TestECCKeysAll(t *testing.T) {
	t.Skip()
	ek := queries.EccKeys{}

	keys, err := ek.SelectAllKeys()
	if err != nil {
		t.Error(err)
		return
	}

	data, err := json.Marshal(keys)
	if err != nil {
		t.Error(err)
		return
	}

	t.Error(string(data))
}

// Use for inserting keys back into db
func TestInsertECCKeys(t *testing.T) {
	t.Skip()
	ek := queries.EccKeys{}
	ecckeys := []common.ECCKeys{}
	err := json.Unmarshal([]byte(dataprod), &ecckeys)
	if err != nil {
		t.Error(err)
	}

	for _, keys := range ecckeys {
		_, err = ek.Insert(keys)
		if err != nil {
			t.Error(err)
		}
	}
}

const dataprod = `[{"ecu":"BCM","pub_key_level_1":"bff0eab780e30ff9ece2fe487d1bab819ddf626ef75fc3886cab3f785180b0360b3dc2a09a64c6a64a2b66415c6438d9811aa8538fcb8d9dd47df3d84a35dfd4","level_1":"be298a33a95f80a782da14b071e49f18e3489f21e3d2e8798a5bc3796e3e78f2","pub_key_level_2":"456b9ed1d87b48c84a8085b59c9d464c842b6c9ab43c38ff86763145ea51613685cfc6fe450b57033a9ac54bd710f6aadb8678b30f49e9679e6abd15d112677b","level_2":"f140a2170d28a3be1f0f4d89627449e2340de90a255137ea621de0c45efc5146","pub_key_level_3":"9d9dbff29ef8bb930010f231d5231a6a9abe88b1db6221381748ad84ee52f3c71b35d45f1f5e051ccde71414b0961a533c9f6ffe0df8c303f43805979d619d8e","level_3":"22d92dcb2dad5436df8274309c1f2e39385733551ffb7cdac4932f14405dc9c9"}]`
