package langs_repo

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

type LangsRepo struct{}

func New() *LangsRepo {
	return &LangsRepo{}
}

func (r *LangsRepo) GetActive() ([]view.Language, error) {
	langs := []view.Language{}
	err := db.DB.Model(model.Language{}).Find(&langs, model.Language{Active: true}).Error
	if err != nil {
		return nil, err
	}
	return langs, nil
}

func (r *LangsRepo) GetAllTranslations() ([]model.Translation, error) {
	var translations []model.Translation
	err := db.DB.Preload("Language").Preload("Scope").Preload("Component").Find(&translations).Error
	if err != nil {
		return nil, err
	}
	return translations, nil
}

func (r *LangsRepo) GetTranslationsByLangID(langID uint) ([]model.Translation, error) {
	var translations []model.Translation
	err := db.DB.Preload("Language").Preload("Scope").Preload("Component").
		Where("lang_id = ?", langID).Find(&translations).Error
	if err != nil {
		return nil, err
	}
	return translations, nil
}

func (r *LangsRepo) GetDefault() (*view.Language, error) {
	var lang view.Language
	err := db.DB.Model(model.Language{}).Where("is_default = ?", true).First(&lang).Error
	if err != nil {
		return nil, err
	}
	return &lang, nil
}

func (r *LangsRepo) GetByISOCode(isoCode string) (*view.Language, error) {
	var lang view.Language
	err := db.DB.Model(model.Language{}).First(&lang, model.Language{ISOCode: isoCode}).Error
	if err != nil {
		return nil, err
	}
	return &lang, nil
}

func (r *LangsRepo) GetById(id uint) (*view.Language, error) {
	var lang view.Language
	err := db.DB.Model(model.Language{}).First(&lang, model.Language{ID: id}).Error
	if err != nil {
		return nil, err
	}
	return &lang, nil
}
