package api

import (
	"fmt"

	def "git.pbiernat.io/egommerce/api-entities/http"
	"github.com/go-redis/redis/v8"
)

func NewBasketAPI(ua string, redis *redis.Client) *BasketAPI {
	return &BasketAPI{NewHttpClient(ua, redis)}
}

type BasketAPI struct {
	httpClient *HttpClient
}

func (a *BasketAPI) GetBasket(basketID string) (*def.GetBasketResponse, error) {
	req := &def.GetBasketRequest{BasketID: basketID}
	res := new(def.GetBasketResponse)
	if err := a.httpClient.SendGet("basket-svc", "/api/v1/basket", req, res); err != nil {
		return nil, err
	}

	return res, nil
}

func (a *BasketAPI) GetBasketItems(basketID string) ([]*def.GetBasketItemsResponse, error) {
	url := fmt.Sprintf("/api/v1/basket/%s/items", basketID)
	var res []*def.GetBasketItemsResponse
	if err := a.httpClient.SendGet("basket-svc", url, nil, &res); err != nil {
		return nil, err
	}

	return res, nil
}