<?php

namespace App\Http\Middleware;

use App\Tenancy\TenantManager;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Blocks tenants whose company is suspended/expired/archived.
 * Must run AFTER ResolveTenant.
 */
class CheckTenantStatus
{
    public function handle(Request $request, Closure $next): Response
    {
        $company = app(TenantManager::class)->current();

        if (! $company) {
            return $next($request); // central context
        }

        return match ($company->status) {
            'archived'  => response('Cont arhivat.', 410),
            'suspended' => response()->view('tenant.suspended', ['company' => $company], 423),
            'expired'   => response()->view('tenant.expired',   ['company' => $company], 402),
            default     => $next($request),
        };
    }
}
