// Code generated by github.com/swaggest/swac <version>, DO NOT EDIT.

package foobar

import (
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
)

// SymbolStatRequest is operation request value.
type SymbolStatRequest struct {
	Aggregate *AggregatorGroup  // Aggregate is an optional `aggregate` parameter in query.
	Symbol    string            // Symbol is a required `symbol` parameter in query.
}

// encode creates *http.Request for request data.
func (request *SymbolStatRequest) encode(ctx context.Context, baseURL string) (*http.Request, error) {
	requestURI := baseURL + "/profile/symbol"

	query := make(url.Values, 2)

	if j, err := json.Marshal(request.Aggregate); err != nil {
		return nil, fmt.Errorf("failed to marshal request parameter 'aggregate': %w", err)
	} else {
		query.Set("aggregate", string(j))
	}

	query.Set("symbol", request.Symbol)

	if len(query) > 0 {
		requestURI += "?" + query.Encode()
	}

	req, err := http.NewRequest(http.MethodGet, requestURI, nil)
	if err != nil {
		return nil, err
	}

	req.Header.Set("Accept", "application/json")

	req = req.WithContext(ctx)

	return req, err
}

// SymbolStatResponse is operation response value.
type SymbolStatResponse struct {
	StatusCode    int
	RawBody       []byte             // RawBody contains read bytes of response body.
	ValueOK       *SymbolStatOutput  // ValueOK is a value of 200 OK response.
	ValueNotFound *RestErrResponse   // ValueNotFound is a value of 404 Not Found response.
}

// decode loads data from *http.Response.
func (result *SymbolStatResponse) decode(resp *http.Response) error {
	var err error

	dump := bytes.NewBuffer(nil)
	body := io.TeeReader(resp.Body, dump)

	result.StatusCode = resp.StatusCode

	switch resp.StatusCode {
	case http.StatusOK:
		err = json.NewDecoder(body).Decode(&result.ValueOK)
		if err != nil {
			err = fmt.Errorf("failed to decode 'get /profile/symbol' OK response: %w", err)
		}
	case http.StatusNotFound:
		err = json.NewDecoder(body).Decode(&result.ValueNotFound)
		if err != nil {
			err = fmt.Errorf("failed to decode 'get /profile/symbol' NotFound response: %w", err)
		}
	default:
		_, readErr := ioutil.ReadAll(body)
		if readErr != nil {
			err = errors.New("unexpected response status: " + resp.Status +
				", could not read response body: " + readErr.Error())
		} else {
			err = errors.New("unexpected response status: " + resp.Status)
		}
	}

	result.RawBody = dump.Bytes()

	if err != nil {
		return responseError{
			resp: resp,
			body: dump.Bytes(),
			err:  err,
		}
	}

	return nil
}

// SymbolStat performs REST operation.
func (c *Client) SymbolStat(ctx context.Context, request SymbolStatRequest) (result SymbolStatResponse, err error) {
	if c.InstrumentCtxFunc != nil {
		ctx = c.InstrumentCtxFunc(ctx, http.MethodGet, "/profile/symbol", &request)
	}

	if c.Timeout != 0 {
		var cancel func()
		ctx, cancel = context.WithTimeout(ctx, c.Timeout)

		defer cancel()
	}

	req, err := request.encode(ctx, c.BaseURL)
	if err != nil {
		return result, err
	}

	resp, err := c.transport.RoundTrip(req)

	if err != nil {
		return result, err
	}

	defer func() {
		closeErr := resp.Body.Close()
		if closeErr != nil && err == nil {
			err = closeErr
		}
	}()

	err = result.decode(resp)

	return result, err
}
