<?php

use Illuminate\Support\Facades\Route;

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

$routes = Route::getRoutes();

$hashes = [];

foreach ($routes as $route) {

    $uri = $route->uri();
    $methods = $route->methods();

    if (!str_starts_with($uri, 'admin')) {
        continue;
    }

    foreach ($methods as $method) {

        if ($method === 'HEAD') continue;

        // --- 1. converte placeholders para [id]
        $mask = preg_replace('/\{[^}]+\}/', '[id]', $uri);

        // --- 2. igual ao middleware
        $string = $method . '|' . $mask;
        $hash = hash('sha256', $string);

        $hashes[] = [
            'method' => $method,
            'uri'    => $uri,
            'masked' => $mask,
            'hash'   => $hash,
        ];
    }
}

file_put_contents(
    storage_path('app/admin_routes_hash.json'),
    json_encode($hashes, JSON_PRETTY_PRINT)
);

echo "OK → Gerado em storage/app/admin_routes_hash.json\n";
