package currency_client

import (
	"github.com/gofiber/fiber/v2"
	"go.uber.org/zap"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/controller/http"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/repository"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
)

type Deps struct {
	CurrencyRepo *repository.CurrencyRepository
	MiddleWare   *http.MiddleWare
	Logger       *zap.Logger
}

type CurrencyController struct {
	currencyRepo *repository.CurrencyRepository
	middleWare   *http.MiddleWare
	logger       *zap.Logger
}

func NewCurrencyController(deps Deps) *CurrencyController {
	return &CurrencyController{
		currencyRepo: deps.CurrencyRepo,
		middleWare:   deps.MiddleWare,
		logger:       deps.Logger,
	}
}

func (receiver *CurrencyController) Get(ctx *fiber.Ctx) error {
	currencyList, err := receiver.currencyRepo.FindCurrenciesList(ctx.Context(), models.DefaultCurrencyListName)
	if err != nil && err.Type() != errors.ErrNotFound {
		return receiver.middleWare.ErrorOld(ctx, err)
	}

	if err != nil && err.Type() == errors.ErrNotFound {
		return ctx.Status(fiber.StatusOK).JSON([]string{})
	}

	return ctx.Status(fiber.StatusOK).JSON(currencyList)
}
