<?php
declare(strict_types=1);
function registerUser(PDO $pdo, string $email, string $password): int {
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($password) < 12) {
        throw new InvalidArgumentException('Check email and password length.');
    }
    $stmt = $pdo->prepare('INSERT INTO users (email, password_hash, created_at) VALUES (:email, :hash, NOW())');
    $stmt->execute(['email' => mb_strtolower($email), 'hash' => password_hash($password, PASSWORD_DEFAULT)]);
    return (int)$pdo->lastInsertId();
}
function verifyLogin(PDO $pdo, string $email, string $password): ?array {
    $stmt = $pdo->prepare('SELECT id,email,password_hash FROM users WHERE email=:email LIMIT 1');
    $stmt->execute(['email' => mb_strtolower(trim($email))]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    if (!$user || !password_verify($password, $user['password_hash'])) return null;
    unset($user['password_hash']);
    return $user;
}
