package routesrepo

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/utils/nullable"
)

type UIRoutesRepo interface {
	GetRoutes(langId uint) ([]model.Route, error)
	GetTopMenu(id uint) ([]model.B2BTopMenu, error)
}

type RoutesRepo struct{}

func New() UIRoutesRepo {
	return &RoutesRepo{}
}

func (p *RoutesRepo) GetRoutes(langId uint) ([]model.Route, error) {
	routes := []model.Route{}
	err := db.DB.Find(&routes, model.Route{Active: nullable.GetNil(true)}).Error
	if err != nil {
		return nil, err
	}
	return routes, nil
}

func (p *RoutesRepo) GetTopMenu(id uint) ([]model.B2BTopMenu, error) {
	var menus []model.B2BTopMenu

	err := db.Get().
		Where("active = ?", 1).
		Order("parent_id ASC, position ASC").
		Find(&menus).Error

	return menus, err
}
