'Content is required']);
exit;
}
$content = $data['content'];
// Wrap content in full HTML if it's just a snippet
if (!preg_match('/
AI Generated App
$content
";
}
try {
$token = bin2hex(random_bytes(16));
$stmt = db()->prepare("INSERT INTO shared_apps (token, content) VALUES (?, ?)");
$stmt->execute([$token, $content]);
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$shareUrl = "$protocol://$host/view.php?t=$token";
echo json_encode([
'success' => true,
'token' => $token,
'url' => $shareUrl
]);
} catch (Exception $e) {
echo json_encode(['error' => $e->getMessage()]);
}