package asana

import (
	"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,gocognit,cyclop,maintidx
	t.Parallel()

	responsePortfolios := testutils.DataFromFile(t, "write-projects.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: "allocations"},
			Server:       mockserver.Dummy(),
			ExpectedErrs: []error{common.ErrMissingRecordData},
		},
		{
			Name:     "Unknown object name is not supported",
			Input:    common.WriteParams{ObjectName: "attributes", RecordData: "dummy"},
			Server:   mockserver.Dummy(),
			Expected: nil,
			ExpectedErrs: []error{
				common.ErrOperationNotSupportedForObject,
			},
		},
		{
			Name:  "Create projects as POST",
			Input: common.WriteParams{ObjectName: "projects", RecordData: map[string]any{"name": "Stuff to buy"}},
			Server: mockserver.Conditional{
				Setup: mockserver.ContentJSON(),
				If:    mockcond.MethodPOST(),
				Then:  mockserver.Response(http.StatusOK, responsePortfolios),
			}.Server(),
			Comparator: testroutines.ComparatorSubsetWrite,
			Expected: &common.WriteResult{
				Success:  true,
				RecordId: "15",
				Errors:   nil,
				Data: map[string]any{
					"gid":           "15",
					"resource_type": "project",
					"name":          "Stuff to buy",
					"archived":      false,
					"color":         "light-green",
					"created_at":    "2012-02-22T02:06:58.147Z",
					"current_status_update": map[string]any{
						"gid":              "12345",
						"resource_type":    "status_update",
						"title":            "Status Update - Jun 15",
						"resource_subtype": "project_status_update",
					},
					"due_on":   "2019-09-15",
					"start_on": "2019-09-14",
				},
			},
			ExpectedErrs: nil,
		},
		{
			Name:  "Update objects as PUT",
			Input: common.WriteParams{ObjectName: "projects", RecordId: "15", RecordData: map[string]any{"name": "Stuff to buy"}}, //nolint:lll
			Server: mockserver.Conditional{
				Setup: mockserver.ContentJSON(),
				If:    mockcond.MethodPUT(),
				Then:  mockserver.Response(http.StatusOK, responsePortfolios),
			}.Server(),
			Comparator: testroutines.ComparatorSubsetWrite,
			Expected: &common.WriteResult{
				Success:  true,
				RecordId: "15",
				Errors:   nil,
				Data: map[string]any{
					"gid":           "15",
					"resource_type": "project",
					"name":          "Stuff to buy",
					"archived":      false,
					"color":         "light-green",
					"created_at":    "2012-02-22T02:06:58.147Z",
					"due_on":        "2019-09-15",
					"start_on":      "2019-09-14",
				},
			},
			ExpectedErrs: nil,
		},
	}

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

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