<?php
session_start();

require 'includes/db.php';
require 'includes/functions.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$user_id = $_SESSION['user_id'];

// 🔥 GET USER (ONLY ONCE)
$user = getUser($user_id);

if (!$user) {
    session_destroy();
    header("Location: login.php");
    exit;
}


// ===============================
// 💰 BTC + LOCAL CURRENCY DISPLAY
// ===============================
require 'includes/settings.php';
require 'includes/currency.php';


// ===============================
// 💱 GET CURRENCY
// ===============================
$user_currency_code = strtoupper($user['currency'] ?? '');

if (empty($user_currency_code)) {

    $currency_cookie = $_COOKIE['currency_selected'] ?? 'USD';

    $map = [
        "R"  => "ZAR",
        "₦"  => "NGN",
        "$"  => "USD",
        "€"  => "EUR",
        "£"  => "GBP"
    ];

    $user_currency_code = $map[$currency_cookie] ?? $currency_cookie;
}

$user_currency_code = strtoupper(trim($user_currency_code));

$allowed = ["USD","ZAR","NGN","EUR","GBP","CHF","CAD","AUD","INR"];

if (!in_array($user_currency_code, $allowed)) {
    $user_currency_code = "USD";
}


// ===============================
// 💱 SYMBOL
// ===============================
$symbols = [
    "USD"=>"$",
    "ZAR"=>"R",
    "NGN"=>"₦",
    "EUR"=>"€",
    "GBP"=>"£",
    "CHF"=>"₣",
    "CAD"=>"$",
    "AUD"=>"$",
    "INR"=>"₹"
];

$symbol = $symbols[$user_currency_code] ?? "";


// ===============================
// 💲 BTC SETTINGS
// ===============================
$btc_amount = getSetting('default_amount_btc');
$btc_rate   = getSetting('btc_rate');

// BTC → USD
$usd_value = $btc_amount * $btc_rate;

// USD → LOCAL
$local_value = convertCurrency($usd_value, 'USD', $user_currency_code);


// ===============================
// 💳 BILLING
// ===============================
$latestBillRes = mysqli_query($conn,"
    SELECT * FROM billings 
    WHERE user_id = '$user_id'
    ORDER BY id DESC LIMIT 1
");

$latestBill = mysqli_fetch_assoc($latestBillRes);

$btcAmount = 0;
$fiatAmount = 0;
$fiatCurrency = '';

if ($latestBill) {
    $btcAmount = $latestBill['amount'];
    $fiatAmount = $latestBill['fiat_amount'];
    $fiatCurrency = $latestBill['fiat_currency'];
}


// ===============================
// 💸 FEE SYSTEM
// ===============================
$res = mysqli_query($conn, "
    SELECT setting_value FROM system_settings 
    WHERE setting_key='account_fee_btc' LIMIT 1
");

$row = mysqli_fetch_assoc($res);
$fee = $row['setting_value'] ?? "0.0010";

// 🔥 FIXED LOCAL FEE CALCULATION
$fee_local = convertCurrency(
    $fee * $btc_rate,
    'USD',
    $user_currency_code
);
?>

<!-- ===============================
     DISPLAY SECTION
================================ -->

<div style="color:#fff; padding:20px;">

    <!-- CUSTOMER ID -->
    <div style="margin-bottom:10px;">
        Customer ID: 
        <strong><?php echo htmlspecialchars($user['customer_id']); ?></strong>
    </div>

    <!-- BTC + LOCAL -->
    <div style="margin-bottom:10px;">
        <strong>
            <?php 
            echo number_format($btc_amount, 6) . " BTC ";
            echo "(~" . $symbol . number_format($local_value, 2) . ")";
            ?>
        </strong>
    </div>

    <!-- FEE -->
    <div>
        Annual Fee: 
        <strong>
            <?php 
            echo $fee . " BTC (~" . $symbol . number_format($fee_local, 2) . ")";
            ?>
        </strong>
    </div>

</div>