package handlers_test

import (
	"net/http"
	"testing"

	"otaupdate/handlers"
	"otaupdate/services"

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

func TestCustomerOtaEmails(t *testing.T) {
	mock := MockDriverEmails{
		SelectResponse: &[]common.DriverEmail{
			{
				Vin:        "VCF1ZBU28PG002114",
				DriverId:   "446b4d69-8768-4e6b-bcf8-0507ea74c952",
				Email:      "ivan.delgadillo@indigotech.com",
				GivenName:  "Ivan",
				FamilyName: "Delgadillo",
			},
			{
				Vin:        "VCF1ZBU25PG003608",
				DriverId:   "7d9c8fed-51b3-4df3-9603-5dfe0e8979f2",
				Email:      "junsub.lee@indigotech.com",
				GivenName:  "Junsub",
				FamilyName: "Lee",
			},
			{
				Vin:        "VCF1EBU2XPG011442",
				DriverId:   "9855e14c-22f6-438d-84ee-47a7be675773",
				Email:      "csimpson@ovloop.com",
				GivenName:  "Clea",
				FamilyName: "Simpson",
			},
		},
	}
	services.GetDB().SetDriverEmails(&mock)

	mocksmtp := smtpclient.MockSMTP{}
	services.SetSMTP(&mocksmtp)

	tests := []mo.DBHttpTest{
		{
			Name: "Good data",
			Request: th.MakeTestRequest(
				http.MethodGet,
				"/customer_ota_emails",
				common.CustomerOtaEmailsRequest{
					VINs:         []string{"VCF1ZBU28PG002114", "VCF1ZBU25PG003608", "VCF1EBU2XPG011442"},
					EmailSubject: "Test Email Subject",
					EmailBody: `Dear Ocean Owner:
					Test Email Body
					Sincerely,
					Me`,
				}),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: ``,
		},
	}

	mo.RunParamHttpTests(t, tests, handlers.HandleCustomerOtaEmails, "/customer_ota_emails", &mock)
}

type MockDriverEmails struct {
	SelectResponse *[]common.DriverEmail
	Error          error
	mo.DBMockHelper
}

func (d *MockDriverEmails) SelectByVINs(vins []string) ([]common.DriverEmail, error) {
	return *d.SelectResponse, d.Error
}
