package salesloft

import (
	"errors"
	"net/http"
	"testing"

	"github.com/amp-labs/connectors"
	"github.com/amp-labs/connectors/common"
	"github.com/amp-labs/connectors/test/utils/mockutils/mockcond"
	"github.com/amp-labs/connectors/test/utils/mockutils/mockserver"
	"github.com/amp-labs/connectors/test/utils/testroutines"
	"github.com/amp-labs/connectors/test/utils/testutils"
)

func TestWrite(t *testing.T) { // nolint:funlen,cyclop
	t.Parallel()

	listSchema := testutils.DataFromFile(t, "write-signals-error.json")
	createAccountRes := testutils.DataFromFile(t, "write-create-account.json")
	createTaskRes := testutils.DataFromFile(t, "write-create-task.json")

	tests := []testroutines.Write{
		{
			Name:         "Write object must be included",
			Server:       mockserver.Dummy(),
			ExpectedErrs: []error{common.ErrMissingObjects},
		},
		{
			Name:         "Write needs data payload",
			Input:        common.WriteParams{ObjectName: "signals"},
			Server:       mockserver.Dummy(),
			ExpectedErrs: []error{common.ErrMissingRecordData},
		},
		{
			Name:  "Correct error message is understood from JSON response",
			Input: common.WriteParams{ObjectName: "signals", RecordId: "22165", RecordData: "dummy"},
			Server: mockserver.Fixed{
				Setup:  mockserver.ContentJSON(),
				Always: mockserver.Response(http.StatusUnprocessableEntity, listSchema),
			}.Server(),
			ExpectedErrs: []error{
				common.ErrBadRequest,
				errors.New("no Signal Registration found for integration id 5167 and given type"), // nolint:goerr113
			},
		},
		{
			Name:  "Write must act as a Create",
			Input: common.WriteParams{ObjectName: "signals", RecordData: "dummy"},
			Server: mockserver.Conditional{
				Setup: mockserver.ContentJSON(),
				If: mockcond.And{
					mockcond.Path("/v2/signals"),
					mockcond.MethodPOST(),
				},
				Then: mockserver.Response(http.StatusOK),
			}.Server(),
			Expected:     &common.WriteResult{Success: true},
			ExpectedErrs: nil,
		},
		{
			Name:  "Write must act as an Update",
			Input: common.WriteParams{ObjectName: "signals", RecordId: "22165", RecordData: "dummy"},
			Server: mockserver.Conditional{
				Setup: mockserver.ContentJSON(),
				If: mockcond.And{
					mockcond.Path("/v2/signals/22165"),
					mockcond.MethodPUT(),
				},
				Then: mockserver.Response(http.StatusOK),
			}.Server(),
			Expected:     &common.WriteResult{Success: true},
			ExpectedErrs: nil,
		},
		{
			Name:  "Valid creation of account",
			Input: common.WriteParams{ObjectName: "accounts", RecordData: "dummy"},
			Server: mockserver.Conditional{
				Setup: mockserver.ContentJSON(),
				If: mockcond.And{
					mockcond.Path("/v2/accounts"),
					mockcond.MethodPOST(),
				},
				Then: mockserver.Response(http.StatusOK, createAccountRes),
			}.Server(),
			Comparator: testroutines.ComparatorSubsetWrite,
			Expected: &common.WriteResult{
				Success:  true,
				RecordId: "1",
				Errors:   nil,
				Data: map[string]any{
					"id":          1.0,
					"name":        "Hogwarts School of Witchcraft and Wizardry",
					"description": "British school of magic for students",
					"country":     "Scotland",
					"counts":      map[string]any{"people": 15.0},
				},
			},
			ExpectedErrs: nil,
		},
		{
			Name:  "Valid creation of a task",
			Input: common.WriteParams{ObjectName: "tasks", RecordData: "dummy"},
			Server: mockserver.Conditional{
				Setup: mockserver.ContentJSON(),
				If: mockcond.And{
					mockcond.Path("/v2/tasks"),
					mockcond.MethodPOST(),
				},
				Then: mockserver.Response(http.StatusOK, createTaskRes),
			}.Server(),
			Comparator: testroutines.ComparatorSubsetWrite,
			Expected: &common.WriteResult{
				Success:  true,
				RecordId: "175204275",
				Errors:   nil,
				Data: map[string]any{
					"subject":       "call me maybe",
					"current_state": "scheduled",
					"task_type":     "call",
				},
			},
			ExpectedErrs: nil,
		},
		{
			Name:  "Valid update of Saved List View",
			Input: common.WriteParams{ObjectName: "saved_list_views", RecordId: "22463", RecordData: "dummy"},
			Server: mockserver.Conditional{
				Setup: mockserver.ContentJSON(),
				If:    mockcond.MethodPUT(),
				Then: mockserver.ResponseString(http.StatusOK, `{"data":{"id":22463,"view":"companies",
					"name":"Hierarchy overview","view_params":{},"is_default":false,"shared":false}}`),
			}.Server(),
			Comparator: testroutines.ComparatorSubsetWrite,
			Expected: &common.WriteResult{
				Success:  true,
				RecordId: "22463",
				Errors:   nil,
				Data: map[string]any{
					"id":   22463.0,
					"name": "Hierarchy overview",
					"view": "companies",
				},
			},
			ExpectedErrs: nil,
		},
	}

	for _, tt := range tests {
		// nolint:varnamelen
		t.Run(tt.Name, func(t *testing.T) {
			t.Parallel()

			tt.Run(t, func() (connectors.WriteConnector, error) {
				return constructTestConnector(tt.Server.URL)
			})
		})
	}
}
