<?php
// app/Traits/HasPermissions.php
namespace App\Traits;

use App\Models\Permission;

trait HasPermissions
{
    /**
     * Get all permissions for the model
     */
    public function getModelPermissions()
    {
        return $this->roles->flatMap(function ($role) {
            return $role->rolePermissions;
        })->unique('id');
    }

    /**
     * Check if model has a specific permission
     */
    public function hasModelPermission($permission): bool
    {
        $permissions = $this->getModelPermissions();
        
        if (is_int($permission)) {
            return $permissions->contains('id', $permission);
        }
        
        if (is_string($permission)) {
            return $permissions->contains('slug', $permission);
        }
        
        if ($permission instanceof Permission) {
            return $permissions->contains('id', $permission->id);
        }
        
        return false;
    }
}