<?php

namespace App\Http\Middleware;

use App\Models\Admin;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class AdminAuth
{
    public function handle(Request $request, Closure $next): Response
    {
        if (!session()->has('admin_id')) {
            return redirect()->route('admin.login')->with('error', 'Please login first.');
        }

        $admin = Admin::find(session('admin_id'));
        if (!$admin || $admin->status !== 'Active') {
            $request->session()->flush();
            return redirect()->route('admin.login')->with('error', 'Session expired. Please login again.');
        }

        view()->share('currentAdmin', $admin);
        return $next($request);
    }
}
