<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class LoginStatusController extends Controller
{
    /**
     * Cek apakah akun dengan email tertentu sedang diblokir.
     * Dibaca oleh halaman login (polling realtime).
     *
     * POST /login/status
     * Body: { email: "user@gmail.com" }
     */
    public function check(Request $request): JsonResponse
    {
        $request->validate(['email' => ['required', 'string', 'email']]);

        $email    = strtolower(trim($request->string('email')));
        $cacheKey = 'login_locked:' . $email;
        $data     = cache()->get($cacheKey);

        $secondsLeft = 0;
        if ($data && isset($data['expires_at'])) {
            $secondsLeft = max(0, $data['expires_at'] - time());
        }

        return response()->json([
            'unlocked'     => $secondsLeft <= 0,
            'seconds_left' => (int) $secondsLeft,
        ]);
    }
}