<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log; // For logging

class AdminAuth
{
    public function handle(Request $request, Closure $next)
    {
        // Debugging: log the request path and session content
        Log::info('AdminAuth Middleware Triggered', [
            'path' => $request->path(),
            'session' => $request->session()->all(),
        ]);

        // Optional: display on screen for immediate debugging
        // echo '<pre>'; print_r($request->session()->all()); echo '</pre>';

        if (!$request->session()->has('admin_id')) {
            Log::info('AdminAuth Middleware: No admin_id in session, redirecting to login.');
            return redirect('/admin/login');
        }

        Log::info('AdminAuth Middleware: admin_id found, proceeding.');
        return $next($request);
    }
}
