<?php
/**
 * TrackPro — Reset All Tracking Data
 * Clears all sessions, analytics, and collected data
 * Keeps: users, settings, link definitions (resets hit counts)
 * DELETE AFTER RUNNING
 */
error_reporting(E_ALL); ini_set('display_errors', 1);

// Safety: require confirmation via URL param
if (($_GET['confirm'] ?? '') !== 'yes') {
    echo '<html><body style="font-family:system-ui;background:#0a0e17;color:#e2e8f0;display:flex;align-items:center;justify-content:center;min-height:100vh">';
    echo '<div style="text-align:center;max-width:500px"><h1 style="color:#ef4444;font-size:1.5rem">⚠ Database Reset</h1>';
    echo '<p style="margin:1rem 0;color:#94a3b8">This will permanently delete ALL tracking data:</p>';
    echo '<ul style="text-align:left;color:#cbd5e1;line-height:2;list-style:none;padding:0">';
    echo '<li>❌ All sessions & visitor data</li>';
    echo '<li>❌ All network, location, device, browser data</li>';
    echo '<li>❌ All fingerprints & behavioral data</li>';
    echo '<li>❌ All risk scores & alerts</li>';
    echo '<li>❌ All screenshots & heatmaps</li>';
    echo '<li>❌ All social detection data</li>';
    echo '<li>❌ All pixel opens</li>';
    echo '<li>❌ All visitor identities</li>';
    echo '<li>✅ Users & settings — KEPT</li>';
    echo '<li>✅ Links — KEPT (hit counts reset to 0)</li>';
    echo '<li>✅ Webhooks & API keys — KEPT</li></ul>';
    echo '<a href="?confirm=yes" style="display:inline-block;margin-top:1.5rem;padding:.8rem 2rem;background:#ef4444;color:#fff;text-decoration:none;border-radius:8px;font-weight:600">Yes, Delete Everything</a>';
    echo '<br><a href="javascript:history.back()" style="display:inline-block;margin-top:1rem;color:#5b6b8a;text-decoration:none">Cancel</a>';
    echo '</div></body></html>';
    exit;
}

$config = require __DIR__ . '/includes/config.php';
$pdo = new PDO("mysql:host={$config['db_host']};dbname={$config['db_name']};charset=utf8mb4", $config['db_user'], $config['db_pass'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

echo "<pre style='font-family:monospace;background:#0a0e17;color:#10b981;padding:2rem;min-height:100vh'>\n";
echo "TrackPro — Resetting All Data\n" . str_repeat('=', 50) . "\n\n";

$pdo->exec("SET FOREIGN_KEY_CHECKS = 0");

$tables = [
    'screenshots',
    'mouse_heatmap',
    'pixel_opens',
    'social_detection',
    'behavioral_events',
    'visitor_sessions',
    'visitor_identities',
    'consent_log',
    'behavioral_data',
    'attribution_data',
    'risk_data',
    'fingerprint_data',
    'browser_data',
    'device_data',
    'location_data',
    'network_data',
    'alerts',
    'sessions',
    'link_tags',
    'tracking_links',
];

foreach ($tables as $t) {
    try {
        $pdo->exec("TRUNCATE TABLE `{$t}`");
        echo "✓ Cleared {$t}\n";
    } catch (PDOException $e) {
        echo "~ {$t} skipped: " . $e->getMessage() . "\n";
    }
}

$pdo->exec("SET FOREIGN_KEY_CHECKS = 1");

// Clear audit log
$pdo->exec("TRUNCATE TABLE `audit_log`");
echo "\n✓ Audit log cleared\n";

echo "\n" . str_repeat('=', 50) . "\n";
echo "DONE — All tracking data has been deleted.\n";
echo "Links, users, settings, webhooks, API keys are preserved.\n\n";
echo ">>> DELETE THIS FILE NOW <<<\n</pre>";
