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"
	"github.com/google/uuid"
)

func TestSubscriptionFeatureGetObject(t *testing.T) {
	mock := mo.MockSubscriptionFeatures{}
	services.GetDB().SetSubFeatures(&mock)
	testFeatureID := uuid.MustParse("ecfb89e0-ca03-4aa9-a43a-a9d703256edb")
	testConfiguration := common.BinaryHex{0x46, 0x44, 0x73, 0x81}
	testDID := common.BinaryHex{0x32, 0x45}
	testPID := common.BinaryHex{0xAA, 0xAA}
	testMask := common.BinaryHex{0x00, 0x00, 0x00, 0x10}
	expectedFilter := &common.SubscriptionFeature{
		ID: testFeatureID,
	}
	expectedResp := `{"id":"ecfb89e0-ca03-4aa9-a43a-a9d703256edb","name":"Test Feature","description":"Test Description","configurations":[{"feature_id":"00000000-0000-0000-0000-000000000000","ecu":"TBOX","sw_version":"SoftwareVersion","hw_version":"HardwareVersion","configuration":"46447381","did":"3245","pid":"aaaa","mask":"00000010"}]}`
	data := common.SubscriptionFeature{
		ID:          testFeatureID,
		Name:        "Test Feature",
		Description: "Test Description",
		Configurations: []common.SubscriptionConfiguration{
			{
				ECU:             "TBOX",
				SoftwareVersion: "SoftwareVersion",
				HardwareVersion: "HardwareVersion",
				Configuration:   &testConfiguration,
				DID:             &testDID,
				PID:             &testPID,
				Mask:            &testMask,
			},
		},
	}

	tests := []mo.DBHttpTest{
		{
			Name:             "No parameters",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionfeature", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
		},
		{
			Name:             "Id parameter",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionfeature?id=ecfb89e0-ca03-4aa9-a43a-a9d703256edb", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: expectedResp,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter:   expectedFilter,
				MockLoadResponse: &data,
			},
		},
		{
			Name:             "Name parameter",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionfeature?name=Test%20Feature", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: expectedResp,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.SubscriptionFeature{
					Name: "Test Feature",
				},
				MockLoadResponse: &data,
			},
		},
		{
			Name:             "Error",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionfeature?id=ecfb89e0-ca03-4aa9-a43a-a9d703256edb", nil),
			ExpectedStatus:   http.StatusServiceUnavailable,
			ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: expectedFilter,
				MockError:      fmt.Errorf("something went wrong"),
			},
		},
	}

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