package handlers_test

import (
	"net/http"
	"testing"
	"time"

	"otaupdate/handlers"
	"otaupdate/services"

	m "github.com/fiskerinc/cloud-services/pkg/common"

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

func TestIssuesGet(t *testing.T) {
	db := services.GetDB()

	mock := mocks.MockIssue{}
	mock.SelectIssuesResponse = []m.Issue{
		{
			ID:          1,
			VIN:         "19XFB2F95DE056700",
			Title:       "test1",
			Description: "",
			DriverID:    "d2ef41d6-d703-4c7a-9a11-ecb9622f9684	",
			Timestamp:   time.Time{},
			IssueImages: []m.IssueImage{},
		},
		{
			ID:          2,
			VIN:         "1XP5D68X4TN494870",
			Title:       "test2",
			Description: "",
			DriverID:    "ada10165-484d-4141-8ad4-794f49537365	",
			Timestamp:   time.Time{},
			IssueImages: []m.IssueImage{},
		},
	}
	db.SetIssues(&mock)

	tests := []th.BasicHttpTest{
		{
			Name:             "Get all",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/issues", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":[{"id":1,"vin":"19XFB2F95DE056700","title":"test1","description":"","driver_id":"d2ef41d6-d703-4c7a-9a11-ecb9622f9684\t","timestamp":"0001-01-01T00:00:00Z"},{"id":2,"vin":"1XP5D68X4TN494870","title":"test2","description":"","driver_id":"ada10165-484d-4141-8ad4-794f49537365\t","timestamp":"0001-01-01T00:00:00Z"}],"total":1}`,
		},
		{
			Name:             "Get by search",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/issues?order=created_at+asc&search=test1", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":[{"id":1,"vin":"19XFB2F95DE056700","title":"test1","description":"","driver_id":"d2ef41d6-d703-4c7a-9a11-ecb9622f9684\t","timestamp":"0001-01-01T00:00:00Z"}],"total":1}`,
		},
	}

	th.RunParamHttpTests(t, tests, handlers.HandleIssuesGet, "/issues")
}
