<?php

require_once __DIR__ . '/app/analytics.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['ok' => false]);
    exit;
}

$raw = file_get_contents('php://input');
$data = json_decode($raw, true);

if (!is_array($data)) {
    $data = $_POST;
}

$event_type = trim($data['event_type'] ?? '');
$entity_type = trim($data['entity_type'] ?? '');
$entity_id = isset($data['entity_id']) && $data['entity_id'] !== '' ? (int) $data['entity_id'] : null;
$entity_title = trim($data['entity_title'] ?? '');
$entity_category = trim($data['entity_category'] ?? '');

sparrow_record_analytics_event(
    $event_type,
    $entity_type,
    $entity_id,
    $entity_title,
    $entity_category
);

echo json_encode(['ok' => true]);