package langsService

import (
	langs_repo "git.ma-al.com/goc_daniel/b2b/app/repos/langsRepo"
	"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/view"
	"github.com/gofiber/fiber/v3"
)

type LangService struct {
	repo langs_repo.LangsRepo
}

type LangServiceMessage i18n.I18nTranslation

const (
	Message_LangsLoaded     LangServiceMessage = "langs_loaded"
	Message_LangsNotLoaded  LangServiceMessage = "langs_not_loaded"
	Message_TranslationsOK  LangServiceMessage = "translations_loaded"
	Message_TranslationsNOK LangServiceMessage = "translations_not_loaded"
)

var LangSrv *LangService

func (s *LangService) GetActive(c fiber.Ctx) response.Response[[]view.Language] {
	res, err := s.repo.GetActive()
	if err != nil {
		c.Status(fiber.StatusBadRequest)
		return response.Make[[]view.Language](nil, 0, i18n.T_(c, response.Message_NOK))
	}
	return response.Make(nullable.GetNil(res), 0, i18n.T_(c, response.Message_OK))
}

// LoadTranslations loads all translations from the database into the cache
func (s *LangService) LoadTranslations() error {
	translations, err := s.repo.GetAllTranslations()
	if err != nil {
		return err
	}
	return i18n.TransStore.LoadTranslations(translations)
}

// ReloadTranslations reloads translations from the database into the cache
func (s *LangService) ReloadTranslations() error {
	translations, err := s.repo.GetAllTranslations()
	if err != nil {
		return err
	}
	return i18n.TransStore.ReloadTranslations(translations)
}

// GetTranslations returns translations from the cache
func (s *LangService) GetTranslations(c fiber.Ctx, langID uint, scope string, components []string) response.Response[*i18n.TranslationResponse] {
	translations, err := i18n.TransStore.GetTranslations(langID, scope, components)
	if err != nil {
		c.Status(fiber.StatusBadRequest)
		return response.Make[*i18n.TranslationResponse](nil, 0, i18n.T_(c, Message_TranslationsNOK))
	}
	return response.Make(nullable.GetNil(translations), 0, i18n.T_(c, Message_TranslationsOK))
}

// GetAllTranslations returns all translations from the cache
func (s *LangService) GetAllTranslationsResponse(c fiber.Ctx) response.Response[*i18n.TranslationResponse] {
	translations := i18n.TransStore.GetAllTranslations()
	return response.Make(nullable.GetNil(translations), 0, i18n.T_(c, Message_TranslationsOK))
}

// ReloadTranslationsResponse returns response after reloading translations
func (s *LangService) ReloadTranslationsResponse(c fiber.Ctx) response.Response[map[string]string] {
	err := s.ReloadTranslations()
	if err != nil {
		c.Status(fiber.StatusInternalServerError)
		return response.Make[map[string]string](nil, 0, i18n.T_(c, Message_LangsNotLoaded))
	}
	result := map[string]string{"status": "success"}
	return response.Make(nullable.GetNil(result), 0, i18n.T_(c, Message_LangsLoaded))
}

// GetDefaultLanguage returns the default language
func (s *LangService) GetDefaultLanguage() (*view.Language, error) {
	return s.repo.GetDefault()
}

// GetLanguageByISOCode returns a language by its ISO code
func (s *LangService) GetLanguageByISOCode(isoCode string) (*view.Language, error) {
	return s.repo.GetByISOCode(isoCode)
}

// GetLanguageByISOCode returns a language by its id
func (s *LangService) GetLanguageById(id uint) (*view.Language, error) {
	return s.repo.GetById(id)
}

func init() {
	LangSrv = &LangService{
		repo: *langs_repo.New(),
	}
}
