<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class CheckUserStatus
{
    public function handle(Request $request, Closure $next): Response
    {
        // Only check status for the web guard (Admin/Employee)
        if (Auth::guard('web')->check()) {
            $user = Auth::guard('web')->user();

            if ($user->status !== 'active') {
                Auth::guard('web')->logout();
                return redirect()->route('login')->with('error', 'تم إيقاف حسابك، يرجى التواصل مع الإدارة');
            }

            // Update last login
            $user->update(['last_login_at' => now()]);
        }

        return $next($request);
    }
}
