package handlers_test

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

	"otaupdate/handlers"
	"otaupdate/services"

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

func TestHandleSupplierActivate(t *testing.T) {
	mock := mocks.MockSupplierAccounts{}
	services.GetDB().SetSupplierAccount(&mock)

	tests := []mocks.DBHttpTest{
		{
			Name:                  "Good data",
			Request:               th.MakeTestRequest(http.MethodPost, "http://example.com/supplier/activate/test@supplier.com", nil),
			ExpectedStatus:        http.StatusOK,
			ExpectedResponseRegex: regexp.MustCompile(`{"activate":"[^"]+"}`),
		},
		{
			Name:             "Error",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/supplier/activate/test@supplier.com", nil),
			ExpectedStatus:   http.StatusServiceUnavailable,
			ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`,
			DBTestCase: mocks.DBTestCase{
				MockError: fmt.Errorf("something went wrong"),
			},
		},
		{
			Name:             "No data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/supplier/activate/", nil),
			ExpectedStatus:   http.StatusNotFound,
			ExpectedResponse: err404NotFound,
		},
		{
			Name:             "Bad Data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/supplier/activate/test", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"email email ","error":"Bad Request"}`,
		},
	}

	mocks.RunParamHttpTests(t, tests, handlers.HandleSupplierActivate, "/supplier/activate/:email", &mock)
}
