package handlers_test

import (
	"fmt"
	"net/http"
	"testing"

	"otaupdate/handlers"
	"otaupdate/services"

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

	"github.com/google/uuid"
)

func TestSubscriptionFeatureUpdate(t *testing.T) {
	mock := mocks.MockSubscriptionFeatures{}
	services.GetDB().SetSubFeatures(&mock)
	testUUID := uuid.MustParse("0557bd1d-76d3-41e5-a44e-13c479e55ab0")
	reqGoodData := common.SubscriptionFeature{
		ID:          testUUID,
		Name:        "Test",
		Description: "Description",
	}

	tests := []mocks.DBHttpTest{
		{
			Name:             "No data",
			Request:          th.MakeTestRequest(http.MethodPut, "http://example.com/subscriptionfeature", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Name required. Description required","error":"Bad Request"}`,
		},
		{
			Name: "Missing PK",
			Request: th.MakeTestRequest(http.MethodPut, "http://example.com/subscriptionfeature", common.SubscriptionFeature{
				Name:        "Test",
				Description: "Description",
			}),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
		},
		{
			Name: "Bad Data",
			Request: th.MakeTestRequest(http.MethodPut, "http://example.com/subscriptionfeature", common.SubscriptionFeature{
				ID: testUUID,
			}),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Name required. Description required","error":"Bad Request"}`,
		},
		{
			Name:             "Good data",
			Request:          th.MakeTestRequest(http.MethodPut, "http://example.com/subscriptionfeature", reqGoodData),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"id":"0557bd1d-76d3-41e5-a44e-13c479e55ab0","name":"Test","description":"Description"}`,
		},
		{
			Name:             "Error",
			Request:          th.MakeTestRequest(http.MethodPut, "http://example.com/subscriptionfeature", reqGoodData),
			ExpectedStatus:   http.StatusServiceUnavailable,
			ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`,
			DBTestCase: mocks.DBTestCase{
				MockError: fmt.Errorf("something went wrong"),
			},
		},
	}

	mocks.RunDBTests(t, tests, handlers.HandleSubscriptionFeatureUpdate, &mock)
}
