<?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
    {
        $user = $request->user();
        abort_unless(
            $user?->can('roles.edit') || $user?->can('platform.superadmin'),
            403,
        );

        $guardName = PermissionGuardName::forApp();

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

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