package handlers_test

import (
	"fmt"
	"net/http"
	"otaupdate/handlers"
	"otaupdate/services"
	"testing"

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

func TestSubscriptionFeatureAdd(t *testing.T) {
	mock := mo.MockSubscriptionFeatures{}
	services.GetDB().SetSubFeatures(&mock)
	validData := common.SubscriptionFeature{
		Name:        "feature_name",
		Description: "description",
	}
	tests := []mo.DBHttpTest{
		{
			Name:             "No data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionfeature", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Name required. Description required","error":"Bad Request"}`,
		},
		{
			Name: "Invalid data",
			Request: th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionfeature", common.SubscriptionFeature{
				Name: "feature_name",
			}),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Description required","error":"Bad Request"}`,
		},
		{
			Name:             "Valid data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionfeature", validData),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"id":"ecfb89e0-ca03-4aa9-a43a-a9d703256edb","name":"feature_name","description":"description"}`,
		},
		{
			Name:             "DB error",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionfeature", validData),
			ExpectedStatus:   http.StatusServiceUnavailable,
			ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`,
			DBTestCase: mo.DBTestCase{
				MockError: fmt.Errorf("something went wrong"),
			},
		},
	}

	mo.RunDBTests(t, tests, handlers.HandleSubscriptionFeatureAdd, &mock)
}
