package websteps

import (
	"context"

	"github.com/ooni/probe-cli/v3/internal/engine/httpx"
	errorsxlegacy "github.com/ooni/probe-cli/v3/internal/engine/legacy/errorsx"
	"github.com/ooni/probe-cli/v3/internal/engine/model"
	"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
)

// Control performs the control request and returns the response.
func Control(
	ctx context.Context, sess model.ExperimentSession,
	thAddr string, resourcePath string, creq CtrlRequest) (out CtrlResponse, err error) {
	clnt := httpx.Client{
		BaseURL:    thAddr,
		HTTPClient: sess.DefaultHTTPClient(),
		Logger:     sess.Logger(),
	}
	// make sure error is wrapped
	err = errorsxlegacy.SafeErrWrapperBuilder{
		Error:     clnt.PostJSON(ctx, resourcePath, creq, &out),
		Operation: errorsx.TopLevelOperation,
	}.MaybeBuild()
	return
}
