<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class RequireAdminPassword
{
    public function handle(Request $request, Closure $next): Response
    {
        if ((bool) $request->session()->get('admin_authenticated', false)) {
            return $next($request);
        }

        if ($request->expectsJson()) {
            return response()->json([
                'message' => 'Admin password required.',
            ], 401);
        }

        return redirect()->route('admin.login');
    }
}
