<?php

namespace KeyLair;

/**
 * A user (claimant/creator) is intentionally never deduplicated by name or
 * email -- two different people can share either, so create() always
 * inserts a fresh row rather than reusing one that happens to match.
 * Picking an existing user instead of creating a new one is a UI choice
 * (a dropdown of existing users), not something this class decides.
 */
class UserFactory
{
    /** Both $name and $email are optional -- neither is required to identify a user; the id is. */
    public static function create(?string $name, ?string $email): int
    {
        $name = $name !== null ? trim($name) : null;
        $email = $email !== null ? trim($email) : null;
        $pdo = Database::connection();
        $stmt = $pdo->prepare('INSERT INTO users (name, email, created_at) VALUES (?, ?, NOW())');
        $stmt->execute([$name !== '' ? $name : null, $email !== '' ? $email : null]);
        return (int) $pdo->lastInsertId();
    }
}
