package handlers_test

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

	"otaupdate/handlers"
	"otaupdate/services"

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

func TestSubscriptionDelete(t *testing.T) {
	results := mocks.MockORMResults{
		ReturnedRows: 0,
		AffectedRows: 1,
	}
	mock := mocks.MockSubscriptions{
		DBMockHelper: mocks.DBMockHelper{
			ORMResponse: &results,
		},
	}
	services.GetDB().SetSubscriptions(&mock)

	tests := []mocks.DBHttpTest{
		{
			Name:             "No id",
			Request:          th.MakeTestRequest(http.MethodDelete, "http://example.com/subscription", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
			DBTestCase: mocks.DBTestCase{
				SetupMockResponse: func() {
					results.AffectedRows = 1
				},
			},
		},
		{
			Name:             "Zero id",
			Request:          th.MakeTestRequest(http.MethodDelete, "http://example.com/subscription?id=0", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
			DBTestCase: mocks.DBTestCase{
				SetupMockResponse: func() {
					results.AffectedRows = 1
				},
			},
		},
		{
			Name:             "Good id",
			Request:          th.MakeTestRequest(http.MethodDelete, "http://example.com/subscription?id=100", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"message":"Deleted"}`,
			DBTestCase: mocks.DBTestCase{
				SetupMockResponse: func() {
					results.AffectedRows = 1
				},
			},
		},
		{
			Name:             "Good vin and sub name",
			Request:          th.MakeTestRequest(http.MethodDelete, "http://example.com/subscription?vin=1G1FP87S3GN100062&name=TEST", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"message":"Deleted"}`,
			DBTestCase: mocks.DBTestCase{
				SetupMockResponse: func() {
					results.AffectedRows = 1
				},
			},
		},
		{
			Name:             "Bad vin",
			Request:          th.MakeTestRequest(http.MethodDelete, "http://example.com/subscription?vin=1G1FP87S3GN100&name=TEST", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
			DBTestCase: mocks.DBTestCase{
				SetupMockResponse: func() {
					results.AffectedRows = 1
				},
			},
		},
		{
			Name:             "Record not found",
			Request:          th.MakeTestRequest(http.MethodDelete, "http://example.com/subscription?vin=1G1FP87S3GN100062&name=TEST", nil),
			ExpectedStatus:   http.StatusNotFound,
			ExpectedResponse: `{"message":"Nothing deleted","error":"Not Found"}`,
			DBTestCase: mocks.DBTestCase{
				SetupMockResponse: func() {
					results.AffectedRows = 0
				},
			},
		},
		{
			Name:             "DB error",
			Request:          th.MakeTestRequest(http.MethodDelete, "http://example.com/subscriptionpackage?id=100", nil),
			ExpectedStatus:   http.StatusServiceUnavailable,
			ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`,
			DBTestCase: mocks.DBTestCase{
				MockError: fmt.Errorf("something went wrong"),
				SetupMockResponse: func() {
					results.AffectedRows = 1
				},
			},
		},
	}

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