<?php

namespace App\Models;

use App\Enums\MenuType;
use App\Enums\Status;
use App\Traits\HasTree;
use Illuminate\Database\Eloquent\Builder;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class AdminMenu extends BaseModel
{
    use HasTree;

    protected $table = 'admin_menus';

    public const CREATED_AT = 'create_time';

    public const UPDATED_AT = 'update_time';

    protected $fillable = [
        'parent_id',
        'name',
        'icon',
        'path',
        'code',
        'component',
        'permission_code',
        'type',
        'visible',
        'sort',
        'status',
        'terminal_type',
        'app_id',
    ];

    protected $casts = [
        'type' => MenuType::class,
        'status' => Status::class,
    ];

    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(AdminRole::class, 'admin_role_menus', 'menu_id', 'role_id');
    }

    public function scopeEnabled(Builder $query): Builder
    {
        return $query->where('status', Status::ENABLED);
    }

    public function scopeVisible(Builder $query): Builder
    {
        return $query->where('visible', 1);
    }
}
