<?php
require_once '../includes/constants.php';
require_once '../includes/session.php';
require_once '../includes/function1.php';

$code = $_GET['code'] ?? '';
$geo = $_GET['geo'] ?? 'US'; // TODO: Replace with IP-to-country detection
$device = $_GET['device'] ?? 'mobile'; // TODO: Replace with user-agent parsing

$code = htmlspecialchars($code);
$geo = htmlspecialchars($geo);
$device = htmlspecialchars($device);

$resolver = new SmartLinkResolver($pdo);
$destination = $resolver->resolve($code, $geo, $device);

if ($destination) {
    header("Location: $destination");
    exit;
} else {
    echo "<p class='error'>❌ No matching SmartLink found for code <strong>$code</strong>.</p>";
}