<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class ResolveTenant
{
    public function handle(Request $request, Closure $next)
    {
        $tenantId = $request->header('X-Tenant-ID') ?? $request->user()?->tenant_id;

        abort_if(! $tenantId, 403, 'Tenant context is required.');

        app()->instance('tenant.id', (int) $tenantId);

        return $next($request);
    }
}
