package queries_test

import (
	"testing"
	"time"

	"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"
)

func TestIssueIntegration(t *testing.T) {
	t.Skip()
	query := setupIssues(t)

	testIssueInsert(t, query)
	testIssueSearch(t, query)
	testIssueSelect(t, query)
	testIssueDelete(t, query)
}

func setupIssues(t *testing.T) queries.Issues {
	instance := queries.Issues{}
	conn = instance.GetDBConn()
	conn.AddQueryHook(db.SQLLogger{})

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

	return instance
}

func testIssueInsert(t *testing.T, query queries.Issues) {

	issue := m.Issue{
		VIN:         "1GNGC26RXXJ407648",
		Title:       "Example HMI Problem",
		Description: "HMI blue screen",
		DriverID:    "0b6b1930-b20a-4fce-967a-efac6a01fd10",
		Timestamp:   time.Now(),
	}

	res, err := query.Insert(&issue)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "Issues Insert", "No error", err)
	}

	if res.RowsAffected() != 1 {
		t.Errorf(testhelper.TestErrorTemplate, "Issues insert RowsAffected", 1, res.RowsAffected())
	}
}

func testIssueSearch(t *testing.T, query queries.Issues) {

	options := queries.PageQueryOptions{
		Offset: 0,
		Limit:  0,
		Order:  "id DESC",
	}

	_, err := query.Search(nil, &options)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "Issues Insert", "No error", err)
	}
}

func testIssueSelect(t *testing.T, query queries.Issues) {

	_, err := query.SelectByID(22)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "Issues Select", "No error", err)
	}
}

func testIssueDelete(t *testing.T, query queries.Issues) {
	_, err := query.Delete(14)
	if err != nil {
		t.Errorf(testhelper.TestErrorTemplate, "Issues Delete", "No error", err)
	}
}
