package handlers_test

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

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

func TestSubscriptionFeatureAssign(t *testing.T) {
	mock := mo.MockSubscriptionPackages{}
	services.GetDB().SetSubPackages(&mock)
	testPackageID := uuid.MustParse("0557bd1d-76d3-41e5-a44e-13c479e55ab0")
	testFeatureID := uuid.MustParse("ecfb89e0-ca03-4aa9-a43a-a9d703256edb")
	validData := handlers.SubFeatureAssignRequest{
		PackageID: testPackageID,
		FeatureID: testFeatureID,
	}
	tests := []mo.DBHttpTest{
		{
			Name:             "No data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionpackagefeature", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"PackageID required. FeatureID required","error":"Bad Request"}`,
		},
		{
			Name: "Invalid data",
			Request: th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionpackagefeature", handlers.SubFeatureAssignRequest{
				PackageID: testPackageID,
			}),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"FeatureID required","error":"Bad Request"}`,
		},
		{
			Name:             "Valid data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionpackagefeature", validData),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"message":"OK"}`,
		},
		{
			Name:             "DB error",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionpackagefeature", 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.HandleSubscriptionFeatureAssign, &mock)
}
