<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureTenantIsActive
{
    public function handle(Request $request, Closure $next): Response
    {
        $tenant = $request->user()?->tenant;

        if (!$tenant || $tenant->status !== 'active' || ($tenant->expires_at && $tenant->expires_at->isPast())) {
            return response()->json(['message' => 'Tenant is inactive, suspended, or expired.'], 403);
        }

        return $next($request);
    }
}
