<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Database;
use App\Core\Request;
use App\Core\Response;

final class RpcController
{
    public function countTentativasFalhas(Request $request): void
    {
        $email = strtolower(trim((string)(
            $request->body['email']
            ?? $request->body['_email']
            ?? $request->body['usuario']
            ?? $request->body['username']
            ?? ''
        )));
        $minutes = (int)($request->body['minutes'] ?? $request->body['_janela_min'] ?? 15);
        $row = Database::fetchOne("SELECT count(*) AS total FROM tentativas_login WHERE lower(email)=lower(:email) AND sucesso=false AND created_at >= now() - (:minutes || ' minutes')::interval", [
            'email' => $email,
            'minutes' => (string)$minutes,
        ]);
        Response::json(['data' => (int)($row['total'] ?? 0)]);
    }
}
