package handlers_test

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

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

func TestUpdateManifestGet(t *testing.T) {
	mock := mo.MockUpdateManifests{}
	services.GetDB().SetUpdateManifests(&mock)
	expectedResp := `{"data":[{"id":1,"name":"Test","version":"1.1","description":"bla bla keyword","rollback":false,"type":"forced","country":"US","powertrain":"MD23","restraint":"None","model":"Ocean","trim":"Sport","year":2022,"body_type":"truck"}],"total":1}`
	expectedRespNoTotal := `{"data":[{"id":1,"name":"Test","version":"1.1","description":"bla bla keyword","rollback":false,"type":"forced","country":"US","powertrain":"MD23","restraint":"None","model":"Ocean","trim":"Sport","year":2022,"body_type":"truck"}]}`
	defaultOrder := "created_at DESC"
	listData := []common.UpdateManifest{
		{
			ID:              1,
			Name:            "Test",
			Version:         "1.1",
			Description:     "bla bla keyword",
			Type:            "forced",
			RollbackEnabled: false,
			Country:         "US",
			PowerTrain:      "MD23",
			Restraint:       "None",
			Model:           "Ocean",
			Trim:            "Sport",
			Year:            2022,
			BodyType:        "truck",
		},
	}

	tests := []mo.DBHttpTest{
		{
			Name:             "No parameters",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/manifests", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: expectedResp,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.UpdateManifest{},
				ExpectedPage: &orm.PageQueryOptions{
					Order:  defaultOrder,
					Limit:  orm.PageQueryOptionsLimitMaximum,
					Offset: 0,
				},
				MockListResponse: listData,
			},
		},
		{
			Name:             "Id parameter",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/manifests?id=1", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: expectedRespNoTotal,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.UpdateManifest{
					ID: 1,
				},
				ExpectedPage: &orm.PageQueryOptions{
					Order:  defaultOrder,
					Limit:  orm.PageQueryOptionsLimitMaximum,
					Offset: 0,
				},
				MockListResponse: listData,
			},
		},
		{
			Name:             "Name, version, description, type parameters",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/manifests?name=Test&version=1.1&desc=keyword&manifest_type=2", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: expectedResp,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.UpdateManifest{
					Name:         "Test",
					Version:      "1.1",
					Description:  "keyword",
					ManifestType: common.SoftwareUpdateType,
					Country:      "US",
					PowerTrain:   "MD23",
					Restraint:    "None",
					Model:        "Ocean",
					Trim:         "Sport",
					Year:         2022,
					BodyType:     "truck",
				},
				ExpectedPage: &orm.PageQueryOptions{
					Order:  defaultOrder,
					Limit:  orm.PageQueryOptionsLimitMaximum,
					Offset: 0,
				},
				MockListResponse: listData,
			},
		},
		{
			Name:             "Paging parameters",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/manifests?offset=10&limit=5", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: expectedRespNoTotal,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.UpdateManifest{},
				ExpectedPage: &orm.PageQueryOptions{
					Order:  defaultOrder,
					Limit:  5,
					Offset: 10,
				},
				MockListResponse: listData,
			},
		},
		{
			Name:             "Error",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/manifests", nil),
			ExpectedStatus:   http.StatusServiceUnavailable,
			ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`,
			DBTestCase: mo.DBTestCase{
				ExpectedFilter: &common.UpdateManifest{},
				ExpectedPage: &orm.PageQueryOptions{
					Order:  defaultOrder,
					Limit:  orm.PageQueryOptionsLimitMaximum,
					Offset: 0,
				},
				MockError: fmt.Errorf("something went wrong"),
			},
		},
		{
			Name:             "Wrong limit, -100",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/manifests?limit=-100", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Limit less than 0","error":"Bad Request"}`,
		},
		{
			Name:             "Wrong limit, 1000",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/manifests?limit=1000", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Limit greater than 100","error":"Bad Request"}`,
		},
		{
			Name:             "SQL Injection Test Simulation",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/manifests?order=CASE WHEN ('1'='1') THEN vin ELSE year END asc", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Order sqlorder ","error":"Bad Request"}`,
		},
	}

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