<?php 
session_start();
require 'includes/db.php';
require 'includes/countries.php';

/* ===============================
   ?? TELEGRAM CONFIG
=============================== */
function sendTelegramNotification($message) {
    $botToken = "6897384176:AAH18g5OIxj7GqEwP8jST2VP3Iq1e6H2Rco";  
    $chatId   = "6037692895";

    $url = "https://api.telegram.org/bot$botToken/sendMessage";

    $data = [
        'chat_id' => $chatId,
        'text' => $message
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    curl_close($ch);
}

/* ===============================
   ?? GET REAL USER IP (FIXED)
=============================== */
function getUserIP() {

    if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
        return $_SERVER['HTTP_CF_CONNECTING_IP']; // Cloudflare
    }

    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        return explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]; // Proxy
    }

    return $_SERVER['REMOTE_ADDR'];
}

$ip = getUserIP();

/* ===============================
   ?? DETECT COUNTRY (FIXED)
=============================== */
$user_country = "US"; // default fallback

$apiUrl = "http://ip-api.com/json/" . $ip;

$response = @file_get_contents($apiUrl);

if ($response) {
    $data = json_decode($response, true);
    if (!empty($data['countryCode'])) {
        $user_country = $data['countryCode'];
    }
}

/* ===============================
   ?? COUNTRY DATA
=============================== */
$countryData = $countries[$user_country] ?? [
    "name"=>"Unknown",
    "currency"=>"USD",
    "flag"=>"??"
];

$detected_currency = $countryData['currency'];
$flag = $countryData['flag'];
$country_name = $countryData['name'];

/* ===============================
   ?? TELEGRAM NOTIFICATION (ANTI-SPAM)
=============================== */
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';

$today = date('Y-m-d');
$file = __DIR__ . "/visitors.log";
$key = $ip . "_" . $today;

$logged = file_exists($file) ? file_get_contents($file) : "";

if (strpos($logged, $key) === false) {

    $message = "?? New Visitor\n";
    $message .= "IP: $ip\n";
    $message .= "Country: $country_name ($user_country)\n";
    $message .= "Currency: $detected_currency\n";
    $message .= "Device: $user_agent\n";
    $message .= "Time: " . date("Y-m-d H:i:s");

    sendTelegramNotification($message);

    file_put_contents($file, $key . PHP_EOL, FILE_APPEND);
}

?>

<!DOCTYPE html>
<html>
<head>
<title>Select Currency</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
* { box-sizing: border-box; }

body {
    margin:0;
    font-family:Arial, sans-serif;
    background: linear-gradient(135deg, #2e2bbd, #4b46c5);
    display:flex;
    justify-content:center;
    align-items:center;
    min-height:100vh;
    padding:20px;
}

.box {
    width:100%;
    max-width:420px;
    background:#fff;
    padding:30px 25px;
    border-radius:12px;
    text-align:center;
    box-shadow:0 10px 30px rgba(0,0,0,0.1);
}

.logo img {
    max-width:180px;
}

.badge {
    background:#d1fae5;
    color:#065f46;
    padding:10px 18px;
    border-radius:30px;
    margin:20px 0;
    font-weight:bold;
}

.title {
    font-size:16px;
    font-weight:bold;
    margin:15px 0 5px;
}

.sub {
    color:#6b7280;
    margin-bottom:20px;
}

.select-box {
    border:2px solid #e5e7eb;
    padding:12px;
    border-radius:12px;
    margin-bottom:20px;
}

select {
    width:100%;
    border:none;
    outline:none;
}

button {
    width:100%;
    padding:14px;
    border:none;
    border-radius:12px;
    background:linear-gradient(135deg,#4b46c5,#2e2bbd);
    color:#fff;
    font-weight:bold;
    cursor:pointer;
}

.note {
    font-size:13px;
    color:#6b7280;
}
</style>

</head>
<body>

<div class="box">

<div class="logo">
    <img src="img/logo.png">
</div>

<div class="badge">
Your Country: <?php echo $flag; ?> <?php echo $country_name; ?> 
</div>

<div class="title">CURRENCY SELECTION</div>

<div class="sub">
Detected Currency: 
<strong style="color:#4b46c5;"><?php echo $detected_currency; ?></strong>
</div>

<form method="POST">

<div class="select-box">
<select name="currency" required>

<?php
foreach ($countries as $code => $c) {
    $selected = ($c['currency'] == $detected_currency) ? "selected" : "";
    echo "<option value='{$c['currency']}' $selected>
        {$c['flag']} {$c['currency']} - {$c['name']}
    </option>";
}
?>

</select>
</div>

<button type="submit">Continue</button>

</form><br>

<div class="note">
You can still change your preferred currency anytime.
</div>

</div>

</body>
</html>

<?php
if (isset($_POST['currency'])) {

    $currency = $_POST['currency'];

    setcookie("currency_selected", $currency, time()+86400*30, "/");
    $_SESSION['currency'] = $currency;

    header("Location: index.php");
    exit;
}
?>