package queries_test

import (
	"errors"
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/common"
	m "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"
	"github.com/google/uuid"
)

func TestSubscriptionTypes(t *testing.T) {
	t.Skip()
	query := setupSubscriptionTypes(t)
	subType := insertSubscriptionType(t, query)
	if subType == nil {
		return
	}
	selectSubscriptionType(t, query, subType.ID)
	updateSubscriptionType(t, query, subType.ID)
	deleteSubscriptionType(t, query, subType.ID)
}

func setupSubscriptionTypes(t *testing.T) queries.SubscriptionTypesInterface {
	instance := &queries.SubscriptionTypes{}
	conn = instance.GetDBConn()
	conn.AddQueryHook(db.SQLLogger{})

	client := instance.GetClient()
	client.InitSchema([]interface{}{
		(*common.SubscriptionType)(nil),
		(*common.Subscription)(nil),
	})

	return instance
}

func insertSubscriptionType(t *testing.T, query queries.SubscriptionTypesInterface) *m.SubscriptionType {
	subtype := common.SubscriptionType{
		Name:          "NAME",
		Destination:   "ICC",
		Description:   "DESC",
		Price:         10099,
		Currency:      "USD",
		DurationValue: 10,
		DurationUnit:  "Hours",
	}

	result, err := query.Insert(&subtype)
	if err != nil {
		result, err := getTestSubscriptionType(query, &subtype)
		if err != nil {
			return nil
		}
		return result
	} else {
		if result.RowsAffected() != 1 {
			t.Errorf(testhelper.TestErrorTemplate, "SubscriptionType insert RowsAffected", 1, result.RowsAffected())
		}
		if result.RowsReturned() != 1 {
			// file insert does not return row
			t.Errorf(testhelper.TestErrorTemplate, "SubscriptionType insert RowsReturned", 1, result.RowsReturned())
		}
	}

	return &subtype
}

func getTestSubscriptionType(query queries.SubscriptionTypesInterface, filter *common.SubscriptionType) (*common.SubscriptionType, error) {
	subtypes, err := query.Select(filter)
	if err != nil {
		return nil, err
	}
	if len(subtypes) == 0 {
		return nil, errors.New("no found")
	}
	return &subtypes[0], nil
}

func selectSubscriptionType(t *testing.T, query queries.SubscriptionTypesInterface, subtypeID uuid.UUID) {
	subtype := common.SubscriptionType{ID: subtypeID}
	err := query.Load(&subtype)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes Load", nil, err)
	}
	if subtype.Name == "" {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes Load Name", "not empty", subtype.Name)
	}

	subTypeValues := map[string]interface{}{
		"Name":          "NAME",
		"Destination":   "ICC",
		"Description":   "DESC",
		"Price":         100.99,
		"Currency":      "USD",
		"DurationValue": 10,
		"DurationUnit":  "Hours",
	}
	testhelper.PropsTester(t, &subtype, subTypeValues)

	subtypes, err := query.Select(&common.SubscriptionType{ID: subtypeID})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes Select", nil, err)
	}
	if len(subtypes) != 1 {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes Select count", 1, len(subtypes))
	} else {
		testhelper.PropsTester(t, &subtypes[0], subTypeValues)
	}

	count, err := query.Count(&common.SubscriptionType{ID: subtypeID})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "UpdateManifest Count", nil, err)
	}
	if count != 1 {
		t.Errorf(testhelper.TestErrorTemplate, "UpdateManifest Count", 1, count)
	}
}

func updateSubscriptionType(t *testing.T, query queries.SubscriptionTypesInterface, subtypeID uuid.UUID) {
	subtype := common.SubscriptionType{ID: subtypeID}
	err := query.Load(&subtype)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes Update Load", nil, err)
	}

	subtype.Name = "NAME2"
	result, err := query.Update(&subtype)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes Update", nil, err)
	}
	if result.RowsAffected() != 1 {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes Update RowsAffected", 1, result.RowsAffected())
	}
	if result.RowsReturned() != 0 {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes Update RowsReturned", 0, result.RowsReturned())
	}

	subtype = common.SubscriptionType{ID: subtypeID}
	err = query.Load(&subtype)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes Update Reload", nil, err)
	}
	if subtype.Name != "NAME2" {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes Update", "NAME2", subtype.Name)
	}
}

func deleteSubscriptionType(t *testing.T, query queries.SubscriptionTypesInterface, subtypeID uuid.UUID) {
	result, err := query.Delete(&common.SubscriptionType{ID: subtypeID})
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes delete", nil, err)
	}

	if result.RowsAffected() != 1 {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes delete RowsAffected", 1, result.RowsAffected())
	}

	if result.RowsReturned() != 0 {
		t.Errorf(testhelper.TestErrorTemplate, "SubscriptionTypes delete RowsReturned", 0, result.RowsReturned())
	}
}
