<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Spatie\Permission\Models\Permission as SpatiePermission;

class Permission extends SpatiePermission
{
    public function scopeForGuard(Builder $query, string $guard = 'web'): Builder
    {
        return $query->where('guard_name', $guard);
    }

    public function moduleName(): string
    {
        $modules = config('permissions.modules', []);

        foreach ($modules as $module => $permissions) {
            if (array_key_exists($this->name, $permissions)) {
                return $module;
            }
        }

        $parts = explode('_', $this->name, 2);

        return $parts[0] ?? 'General';
    }
}
