<?php

namespace App\Http\Middleware;

use App\Voter;
use Closure;
use Illuminate\Support\Facades\Auth;

class FizikalRegistrationVerifiedMiddleware
{
    /**
     * Block voting until fizikal attendance is confirmed by admin (kehadiran = 1 + verified_at set).
     */
    public function handle($request, Closure $next)
    {
        $user = Auth::user();

        if (!$user instanceof Voter) {
            return response()->json([
                'status' => 'failed',
                'message' => 'Sesi tidak sah.',
            ], 401);
        }

        if (!$user->isFizikalRegistrationVerifiedForVoting()) {
            return response()->json([
                'status' => 'failed',
                'message' => 'Pendaftaran fizikal anda belum disahkan oleh pentadbir. Sila tunggu pengesahan di kaunter.',
            ], 403);
        }

        return $next($request);
    }
}
