<?php

namespace App\Repositories;

use App\Contracts\PermissionRepositoryInterface;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Database\Eloquent\Collection;

class PermissionRepository implements PermissionRepositoryInterface
{
    public function getAllPermissionsGroupedByModule(): array
    {
        return Permission::all()->groupBy('module')->toArray();
    }

    public function getAllRolesWithPermissions(): Collection
    {
        return Role::with('permissions')->get();
    }

    public function updateRolePermissions(int $roleId, array $permissionIds): bool
    {
        $role = Role::findOrFail($roleId);
        $role->permissions()->sync($permissionIds);
        return true;
    }
}
