<?php
// app/Http/Middleware/DoctorAuthMiddleware.php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class DoctorAuthMiddleware
{
    public function handle(Request $request, Closure $next): mixed
    {
        if (! session('doctor_id')) {
            return redirect()->route('login')
                ->with('error', 'يجب تسجيل الدخول أولاً');
        }

        $doctor = \App\Models\Doctor::with('user')->find(session('doctor_id'));

        if (! $doctor || $doctor->status !== 'active') {
            session()->forget('doctor_id');
            return redirect()->route('login')
                ->with('error', 'الحساب غير نشط');
        }

        view()->share('currentDoctor', $doctor);
        return $next($request);
    }
}
