<?php

namespace App\Models;

use App\Traits\NodeTrait;
use App\Traits\ModelTrait;

class Nav extends NodeTree
{
    use ModelTrait, NodeTrait;

    protected $table = 'navs';

    protected $guarded = ['description', 'id', 'parent_id', 'lft', 'rgt', 'depth'];
    
    protected $orderColumn = 'orders';

    public function description()
    {
        $lang = config('lang') ?: config('setting.lang');

        if (config('visit') == 'frontend' && config('lang') != config('setting.lang')) {
            return $this->hasOne(NavDescription::class)->whereIn('lang', [config('lang'), config('setting.lang')])->orderByRaw(\DB::raw("FIELD(lang, '". config('lang') ."', '". config('setting.lang') ."')"));
        } elseif (config('visit') == 'admin' && config('managerLang') != config('setting.lang')) {
            return $this->hasOne(NavDescription::class)->whereIn('lang', [config('managerLang'), config('setting.lang')])->orderByRaw(\DB::raw("FIELD(lang, '". config('managerLang') ."', '". config('setting.lang') ."')"));
        }

        return $this->hasOne(NavDescription::class)->where('lang', $lang);
    }
}
