<?php

namespace Maestro\Core\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maestro\Core\Support\PermissionGuardName;
use Spatie\Permission\Models\Permission;

class PermissionListController extends Controller
{
    /**
     * All permission names for the app’s default Spatie guard (role editor).
     */
    public function index(Request $request): JsonResponse
    {
        abort_unless($request->user()?->can('roles.edit'), 403);

        $guardName = PermissionGuardName::forApp();

        $names = Permission::query()
            ->where('guard_name', $guardName)
            ->orderBy('name')
            ->pluck('name')
            ->values();

        return response()->json(['data' => $names]);
    }
}
