<?php
require_once __DIR__ . '/admin/db.php';
header('Content-Type: text/plain; charset=utf-8');

$landing_id = (int)($_POST['landing_id'] ?? $_GET['landing_id'] ?? 0);
$variant_id = (int)($_POST['variant_id'] ?? $_GET['variant_id'] ?? 0);
$event_type = $_POST['event_type'] ?? $_GET['event_type'] ?? 'conversion';
$session_id = $_POST['session_id'] ?? $_GET['session_id'] ?? null;

if($landing_id<=0 || !in_array($event_type, ['impression','conversion'], true)) { http_response_code(400); echo "bad"; exit; }

$vid = $variant_id>0 ? $variant_id : null;
$ip  = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? null;
$ua  = $_SERVER['HTTP_USER_AGENT'] ?? null;

$st = $conn->prepare("INSERT INTO landing_variant_stats (landing_id, variant_id, event_type, session_id, ip_address, user_agent) VALUES (?,?,?,?,?,?)");
$st->bind_param('iissss', $landing_id, $vid, $event_type, $session_id, $ip, $ua);
$st->execute();

echo "ok";
