package categoryrepo

import (
	"git.ma-al.com/goc_daniel/b2b/app/db"
	"git.ma-al.com/goc_daniel/b2b/app/model/dbmodel"
)

type UICategoryRepo interface {
	GetCategoryTranslations(ids []uint, idLang uint) (map[uint]string, error)
}

type CategoryRepo struct{}

func New() UICategoryRepo {
	return &CategoryRepo{}
}

func (r *CategoryRepo) GetCategoryTranslations(ids []uint, idLang uint) (map[uint]string, error) {
	if len(ids) == 0 {
		return map[uint]string{}, nil
	}

	type result struct {
		IDCategory int32
		Name       string
	}

	var results []result
	if err := db.Get().
		Model(dbmodel.PsCategoryLang{}).
		Select("ps_category_lang.id_category, COALESCE(ps_category_lang.name, '') as name").
		Where("ps_category_lang.id_category IN ?", ids).
		Where("ps_category_lang.id_lang = ?", idLang).
		Scan(&results).Error; err != nil {
		return nil, err
	}

	translations := make(map[uint]string, len(results))
	for _, res := range results {
		translations[uint(res.IDCategory)] = res.Name
	}

	return translations, nil
}
