<?php
session_start();
include '../config/db.php';
include '../config/function.php';
include '../config/config.php';

function getCryptoPrices() {
    $cacheFile = 'crypto_prices_cache.json';
    $cacheTime = 300; // 5 minutes cache

    if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $cacheTime) {
        return json_decode(file_get_contents($cacheFile), true);
    }

    $url = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,litecoin,solana&vs_currencies=usd';
    $response = @file_get_contents($url);

    if ($response) {
        file_put_contents($cacheFile, $response);
        return json_decode($response, true);
    }
    return false;
}

if (isset($_SESSION["USER_LOG"]) && $_SESSION['USER_LOG'] != "") {
    $user = $_SESSION['USER_LOG'];
    $Get = mysqli_query($conn, "SELECT * FROM users WHERE email = '$user' ");
    
    if (mysqli_num_rows($Get) > 0) {
        $row = mysqli_fetch_assoc($Get);
        
        $user_id = $row['id'];
        $uid = $row['id'];
        $first_name = $row['first_name'];
        $last_name = $row['last_name'];
        $full_name = $row['first_name'] . ' ' . $row['last_name'];
        $email = $row['email'];
        $phone_no = $row['phone_no'];
        $street_address  = $row['street_address'];
        $state = $row['state'];
        $country = $row['country'];
        $gender = $row['gender'];
        $dob = $row['dob'];
        $status = $row['status'];
        $account_id = $row['account_id'];
        $profile_picture = $row['profile_picture'];
        
        $balance = $row['balance'];
        $profit = $row['profit'];
        
        $btc_add = $row['btc_add'];
        $eth_add = $row['eth_add'];
        
        // USDT amounts for each coin from database
        $btc_usdt = $row['btc_bal'];  // BTC amount in USDT value
        $eth_usdt = $row['eth_bal'];  // ETH amount in USDT value
        $ltc_usdt = $row['ltc_bal'];  // LTC amount in USDT value
        $sol_usdt = $row['sol_bal'];  // SOL amount in USDT value
        
        $btc_usdt_demo = $row['btc_bal_demo'];  // BTC amount in USDT value for demo
        
        
        
        $total_coinusdt_amount =  $btc_usdt + $eth_usdt + $ltc_usdt +  $sol_usdt;
        
        // Get current prices
        $prices = getCryptoPrices();
        
        if ($prices) {
            // Calculate equivalent coin amounts
            $btc_amount_demo = $btc_usdt_demo / $prices['bitcoin']['usd'];
              
              
            $btc_amount = $btc_usdt / $prices['bitcoin']['usd'];
            $eth_amount = $eth_usdt / $prices['ethereum']['usd'];
            $ltc_amount = $ltc_usdt / $prices['litecoin']['usd'];
            $sol_amount = $sol_usdt / $prices['solana']['usd'];
            
          
            
        } else {
            // echo '<div class="error">Could not fetch current cryptocurrency prices. Please try again later.</div>';
        }
    } else {
        header("Location: ../auth/login.php");
        exit;
    }
} else {
    header("Location: ../auth/login.php");
    exit;
}
?>