package restricted

import (
	"git.ma-al.com/goc_daniel/b2b/app/service/localeSelectorService"
	"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
	"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
	"git.ma-al.com/goc_daniel/b2b/app/utils/response"
	"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
	"github.com/gofiber/fiber/v3"
)

// LocaleSelectorHandler for getting languages and countries data
type LocaleSelectorHandler struct {
	localeSelectorService *localeSelectorService.LocaleSelectorService
}

// NewLocaleSelectorHandler creates a new LocaleSelectorHandler instance
func NewLocaleSelectorHandler() *LocaleSelectorHandler {
	localeSelectorService := localeSelectorService.New()
	return &LocaleSelectorHandler{
		localeSelectorService: localeSelectorService,
	}
}

func LocaleSelectorHandlerRoutes(r fiber.Router) fiber.Router {
	handler := NewLocaleSelectorHandler()

	r.Get("/get-languages", handler.GetLanguages)
	r.Get("/get-countries", handler.GetCountries)

	return r
}

func (h *LocaleSelectorHandler) GetLanguages(c fiber.Ctx) error {
	languages, err := h.localeSelectorService.GetLanguages()
	if err != nil {
		return c.Status(responseErrors.GetErrorStatus(err)).
			JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
	}

	return c.JSON(response.Make(&languages, 0, i18n.T_(c, response.Message_OK)))
}

func (h *LocaleSelectorHandler) GetCountries(c fiber.Ctx) error {
	countries, err := h.localeSelectorService.GetCountriesAndCurrencies()
	if err != nil {
		return c.Status(responseErrors.GetErrorStatus(err)).
			JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
	}

	return c.JSON(response.Make(&countries, 0, i18n.T_(c, response.Message_OK)))
}
