<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Role extends Model
{
    use HasFactory;

    protected $table = 'adminlte_roles';

    protected $fillable = [
        'name',
        'label',
    ];

    /**
     * Users that are assigned this role.
     */
    public function users(): BelongsToMany
    {
        return $this->belongsToMany({{ users_model_ref }}::class, 'adminlte_role_user');
    }

    /**
     * Permissions granted to this role.
     */
    public function permissions(): BelongsToMany
    {
        return $this->belongsToMany(Permission::class, 'adminlte_permission_role');
    }

    /**
     * Determine whether the role has the given permission.
     */
    public function hasPermission(string $name): bool
    {
        return $this->permissions()->where('name', $name)->exists();
    }
}
