package client

import (
	"bytes"
	"context"
	"encoding/json"
	"net/http"

	"github.com/pkg/errors"
	"gitlab.com/wpetit/goweb/api"
	"gitlab.com/wpetit/goweb/logger"
)

func (c *Client) apiGet(ctx context.Context, url string, result any, funcs ...HTTPOptionFunc) error {
	if err := c.apiDo(ctx, http.MethodGet, url, nil, result, funcs...); err != nil {
		return errors.WithStack(err)
	}

	return nil
}

func (c *Client) apiPost(ctx context.Context, url string, payload any, result any, funcs ...HTTPOptionFunc) error {
	if err := c.apiDo(ctx, http.MethodPost, url, payload, result, funcs...); err != nil {
		return errors.WithStack(err)
	}

	return nil
}

func (c *Client) apiDelete(ctx context.Context, url string, payload any, result any, funcs ...HTTPOptionFunc) error {
	if err := c.apiDo(ctx, http.MethodDelete, url, payload, result, funcs...); err != nil {
		return errors.WithStack(err)
	}

	return nil
}

func (c *Client) apiDo(ctx context.Context, method string, url string, payload any, response any, funcs ...HTTPOptionFunc) error {
	opts := NewHTTPOptions(funcs...)

	logger.Debug(
		ctx, "new http request",
		logger.F("method", method),
		logger.F("url", url),
		logger.F("payload", payload),
	)

	var buf bytes.Buffer

	encoder := json.NewEncoder(&buf)

	if err := encoder.Encode(payload); err != nil {
		return errors.WithStack(err)
	}

	req, err := http.NewRequest(method, url, &buf)
	if err != nil {
		return errors.WithStack(err)
	}

	for key, values := range opts.Headers {
		for _, v := range values {
			req.Header.Add(key, v)
		}
	}

	res, err := c.http.Do(req)
	if err != nil {
		return errors.WithStack(err)
	}

	defer res.Body.Close()

	decoder := json.NewDecoder(res.Body)

	if err := decoder.Decode(&api.Response{Data: &response}); err != nil {
		return errors.WithStack(err)
	}

	return nil
}