package mocks

import (
	"fmt"
	"testing"

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

type DBTestCase struct {
	ExpectedFilter    fmt.Stringer
	ExpectedPage      *queries.PageQueryOptions
	MockListResponse  interface{}
	MockLoadResponse  interface{}
	SetupMockResponse func()
	MockError         error
	MockDriverError   error
}

func (tc *DBTestCase) SetupDB(mock DBMockHelperInterface) {
	if mock != nil {
		mock.SetErr(tc.MockError)
		mock.SetListResp(tc.MockListResponse)
		mock.SetLoadResp(tc.MockLoadResponse)
		mock.SetDriverError(tc.MockDriverError)
	}

	if tc.SetupMockResponse != nil {
		tc.SetupMockResponse()
	}
}

func (tc *DBTestCase) Validate(t *testing.T, name string, mock DBMockHelperInterface) {
	if mock != nil {
		if mock.GetFilter() != nil && tc.ExpectedFilter != nil && mock.GetFilter().String() != tc.ExpectedFilter.String() {
			t.Errorf(th.TestErrorTemplate, name, tc.ExpectedFilter.String(), mock.GetFilter().String())
		} else if mock.GetFilter() == nil && tc.ExpectedFilter != nil {
			t.Errorf(th.TestErrorTemplate, name, tc.ExpectedFilter.String(), nil)
		}

		if mock.GetPaging() != nil && tc.ExpectedPage != nil && mock.GetPaging().String() != tc.ExpectedPage.String() {
			t.Errorf(th.TestErrorTemplate, name, tc.ExpectedPage.String(), mock.GetPaging().String())
		} else if mock.GetPaging() == nil && tc.ExpectedPage != nil {
			t.Errorf(th.TestErrorTemplate, name, tc.ExpectedPage.String(), nil)
		}
	}
}
