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, roleId 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(langId uint, roleId uint) ([]model.B2BTopMenu, error) {
	var menus []model.B2BTopMenu

	err := db.
		Get().
		Model(model.B2BTopMenu{}).
		Joins("JOIN b2b_top_menu_roles tmr ON tmr.top_menu_id = b2b_top_menu.menu_id").
		Where(model.B2BTopMenu{Active: 1}).
		Where("tmr.role_id = ?", roleId).
		Order("b2b_top_menu.parent_id ASC, b2b_top_menu.position ASC").
		Find(&menus).Error

	return menus, err
}
