<?php
require 'db.php';
header('Content-Type: application/json');

$email = $_POST['email'] ?? '';
$code = $_POST['code'] ?? '';

$stmt = $pdo->prepare("SELECT id FROM users WHERE email=? AND verification_code=? AND code_created_at > NOW()");
$stmt->execute([$email, $code]);
$user = $stmt->fetch();

if(!$user){
    echo json_encode(["status"=>"error"]);
    exit;
}

$hashed=1;

$stmt = $pdo->prepare("UPDATE users SET is_verified=?, verification_code=NULL, code_created_at=NULL WHERE id=?");
$stmt->execute([$hashed, $user['id']]);

 echo json_encode(["status"=>"success"]);