package api

import (
	"strconv"

	"git.ma-al.com/goc_daniel/b2b/app/config"
	"git.ma-al.com/goc_daniel/b2b/app/service/langsService"
	"github.com/gofiber/fiber/v3"
)

type LangHandler struct {
	service langsService.LangService
}

func NewLangHandler() *LangHandler {
	return &LangHandler{
		service: *langsService.LangSrv,
	}
}

func (h *LangHandler) InitLanguage(api fiber.Router, cfg *config.Config) {
	api.Get("/langs", h.GetLanguages)
	api.Get("/translations", h.GetTranslations)
	api.Get("/translations/reload", h.ReloadTranslations)
}

func (h *LangHandler) GetLanguages(c fiber.Ctx) error {
	return c.JSON(h.service.GetActive(c))
}

func (h *LangHandler) GetTranslations(c fiber.Ctx) error {
	langIDStr := c.Query("lang_id", "0")
	langID, _ := strconv.Atoi(langIDStr)
	scope := c.Query("scope", "")
	componentsStr := c.Query("components", "")

	var components []string
	if componentsStr != "" {
		components = []string{componentsStr}
	}

	return c.JSON(h.service.GetTranslations(c, uint(langID), scope, components))
}

func (h *LangHandler) ReloadTranslations(c fiber.Ctx) error {
	return c.JSON(h.service.ReloadTranslationsResponse(c))
}
