package testclient

import (
	"context"
	"net/http"

	v1 "github.com/AlchemyTelcoSolutions/callisto-so-bff/api/v1"
	"github.com/AlchemyTelcoSolutions/callisto-so-bff/integration_tests/fakes"
	"github.com/AlchemyTelcoSolutions/callisto-so-bff/internal/proxy"
)

// PostSaleOrder is a integration test function for UpdateSaleOrder
func (c Client) PostSaleOrderLegacy(apicase string) (*v1.PostUpdateSaleOrdersRestResponse, error) {
	request := getSORequestUpdate()

	if apicase == fakes.SuccessNetsuiteUpdate {
		request = getSORequestUpdateNetsuite()
	}

	// add legacy order id in context
	ctx := context.WithValue(context.Background(), proxy.LegacyOrderIDCtxValue{}, apicase)

	w := newCustomResponseWriter()
	req := newRequestBuilder(http.MethodPost)
	req, err := req.withMultipartFormData(nil, request)
	if err != nil {
		return nil, err
	}
	r := req.getRequest().WithContext(ctx)
	c.client.UpdateSaleOrder(w, r, apicase)
	var resp v1.PostUpdateSaleOrdersRestResponse
	o, err := InterceptBody(w, resp)
	return o, err
}

func getSORequestUpdate() []fieldData {
	return []fieldData{
		{
			field:   "_method",
			content: "PUT",
		},
		{
			field:   "doc_type_id",
			content: "1",
		},
		{
			field:   "note",
			content: "this is a note",
		},
		{
			field:   "shipped_date",
			content: "2022-12-05 01:02:03",
		},
		{
			field:   "collect_date",
			content: "2022-12-05 01:02:03",
		},
		{
			field:   "tracking_code",
			content: "this_is_tracking_code",
		},
		{
			field:   "shipping_method_id",
			content: "2",
		},
		{
			field:   "tracking_url",
			content: "http://tracking.com",
		},
		{
			field:   "no_airbill",
			content: "1",
		},
		{
			field:   "courier_name",
			content: "DHL",
		},
	}
}

func getSORequestUpdateNetsuite() []fieldData {
	return []fieldData{
		{
			field:   "_method",
			content: "POST",
		},
		{
			field:   "status",
			content: "3",
		},
		{
			field:   "type",
			content: "pi",
		},
	}
}
