<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class EnsureActiveSubscription
{
    public function handle(Request $request, Closure $next)
    {
        $active = DB::table('subscriptions')
            ->where('tenant_id', app('tenant.id'))
            ->where('status', 'active')
            ->whereDate('expires_at', '>=', now())
            ->exists();

        abort_if(! $active, 402, 'Subscription is inactive or expired.');

        return $next($request);
    }
}
