package handlers_test

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

	"otaupdate/handlers"
	"otaupdate/services"

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

func TestHandleCarUpdatesLog(t *testing.T) {
	date := time.Date(2021, time.November, 10, 23, 0, 0, 0, time.UTC)
	mock := mo.MockCarUpdates{
		SelectCarUpdateStatusesResponse: []common.CarUpdateStatus{
			{
				ID:          1000,
				CarUpdateID: 100,
				Status:      "pending",
				DBModelBase: dbbasemodel.DBModelBase{
					CreatedAt: &date,
					UpdatedAt: &date,
				},
			},
		},
	}
	services.GetDB().SetCarUpdates(&mock)
	//year int, month Month, day, hour, min, sec, nsec int, loc *Location
	tests := []th.BasicHttpTest{
		{
			Name:             "Missing query",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/carupdateslog", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"car update id required","error":"Bad Request"}`,
		},
		{
			Name:             "Bad car update ids",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/carupdateslog?carupdateid=XXXXXXXXX", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"invalid id XXXXXXXXX","error":"Bad Request"}`,
		},
		{
			Name:             "Good request",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/carupdateslog?carupdateid=100", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":[{"id":1000,"carupdate_id":100,"status":"pending","error_code":0,"created":"2021-11-10T23:00:00Z","updated":"2021-11-10T23:00:00Z"}],"total":1}`,
		},
	}

	th.RunBasicHttpTests(t, tests, handlers.HandleCarUpdatesLog)
}
