<?php
declare(strict_types=1);

function track_event(PDO $pdo, string $event, ?int $productId = null, ?int $orderId = null): void {
    $stmt = $pdo->prepare("
        INSERT INTO analytics_events (event_name, page, product_id, order_id, ip_address, user_agent, referer)
        VALUES (?, ?, ?, ?, ?, ?, ?)
    ");

    $stmt->execute([
        $event,
        $_SERVER['REQUEST_URI'] ?? null,
        $productId,
        $orderId,
        $_SERVER['REMOTE_ADDR'] ?? null,
        $_SERVER['HTTP_USER_AGENT'] ?? null,
        $_SERVER['HTTP_REFERER'] ?? null,
    ]);
}
