<?php

// --- CONFIGURATION ---
$TELEGRAM_BOT_TOKEN = '8158030568:AAGjv9Pefvj9Kggo9VxLbRn1AT7wAX7QDF8';
$TELEGRAM_CHAT_ID   = '-5269491578';
// ---------------------

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

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

if (!$request || !isset($request['type']) || $request['type'] !== 'download') {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request']);
    exit;
}

$data     = $request['data'];
$timeOnly = date('H:i:s');

$message = "📥 <b>Download Initiated</b>
👤 <b>Visitor #" . intval($data['visitorNumber']) . "</b>
🕒 <b>Download Time:</b> {$timeOnly}
🌐 <b>IP:</b> " . htmlspecialchars($data['ip']) . "
🌍 <b>Country:</b> " . htmlspecialchars($data['country'] ?? 'Unknown') . "
🏢 <b>ISP:</b> " . htmlspecialchars($data['isp'] ?? 'Unknown') . "
🤖 <b>User Type:</b> " . htmlspecialchars($data['botResult']) . "
💻 <b>OS:</b> " . htmlspecialchars($data['os']) . "
🔗 <b>File:</b> " . htmlspecialchars($data['link']) . "
✅ <b>Status:</b> Download completed successfully";

$url      = "https://api.telegram.org/bot{$TELEGRAM_BOT_TOKEN}/sendMessage";
$postData = [
    'chat_id'    => $TELEGRAM_CHAT_ID,
    'text'       => $message,
    'parse_mode' => 'HTML'
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
curl_close($ch);

echo json_encode(['status' => 'success', 'telegram_response' => json_decode($response)]);
?>
