<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
final class AssignRequestId {
 public function handle(Request $request, Closure $next) {
  $incoming=$request->header('X-Request-Id');
  $id=is_string($incoming)&&preg_match('/^req_[A-Za-z0-9_-]{8,100}$/',$incoming)?$incoming:'req_'.Str::ulid();
  $request->attributes->set('request_id',$id);
  $response=$next($request); $response->headers->set('X-Request-Id',$id); $response->headers->set('X-Api-Version','2026-07-01'); return $response;
 }
}
