<?php  
session_start();  
require_once 'db.php';  
  
if (!isset($_SESSION['user_id'])) {  
  $_SESSION['invest_message'] = "Please login to invest.";  
  header("Location: investment.php");  
  exit;  
}  
  
$user_id = $_SESSION['user_id'];  
  
if (isset($_POST['amount']) && isset($_POST['cycle_days']) && isset($_POST['daily_income'])) {  
  $amount = floatval($_POST['amount']);  
  $cycle_days = intval($_POST['cycle_days']);  
  $daily_income = floatval($_POST['daily_income']);  
  
  if ($amount <= 0 || $cycle_days <= 0 || $daily_income <= 0) {  
    $_SESSION['invest_message'] = "Invalid input values.";  
    header("Location: investment.php");  
    exit;  
  }  
  
  // Calculate daily percentage  
  $daily_percentage = ($daily_income / $amount) * 100;  
  $start_time = date('Y-m-d H:i:s');  
  $status = 'active';  
  $last_earnings_time = $start_time;  
  $total_earned = 0;  
  
  // Check wallet balance  
  $stmt = $conn->prepare("SELECT balance FROM users WHERE id = ?");  
  $stmt->bind_param("i", $user_id);  
  $stmt->execute();  
  $result = $stmt->get_result();  
  $user = $result->fetch_assoc();  
  
  if (!$user || $user['balance'] < $amount) {  
    $_SESSION['invest_message'] = "Insufficient balance.";  
    header("Location: vip.php");  
    exit;  
  }  
  
  // Deduct balance  
  $stmt = $conn->prepare("UPDATE users SET balance = balance - ? WHERE id = ?");  
  $stmt->bind_param("di", $amount, $user_id);  
  $stmt->execute();  
  
  // Insert investment  
  $stmt = $conn->prepare("INSERT INTO investments (user_id, amount, start_time, status, daily_percentage, last_earnings_time, daily_earnings, total_earned, cycle_days)  
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");  
  $stmt->bind_param("idsssdddi", $user_id, $amount, $start_time, $status, $daily_percentage, $last_earnings_time, $daily_income, $total_earned, $cycle_days);  
  $stmt->execute();  
  
  // === Referral Bonuses ===  
  $referral_percentages = [0.15, 0.01, 0.01]; // Level 1 = 12%, Level 2 = 2%, Level 3 = 1%  
  $current_user = $user_id;  
  
  foreach ($referral_percentages as $level => $percent) {  
    // Get upline  
    $stmt = $conn->prepare("SELECT referred_by FROM users WHERE id = ?");  
    $stmt->bind_param("i", $current_user);  
    $stmt->execute();  
    $result = $stmt->get_result();  
    $row = $result->fetch_assoc();  
  
    if (!$row || !$row['referred_by']) break; // no more uplines  
  
    $upline_code = $row['referred_by'];  
  
    // Get upline user id  
    $stmt = $conn->prepare("SELECT id FROM users WHERE invite_code = ?");  
    $stmt->bind_param("s", $upline_code);  
    $stmt->execute();  
    $result = $stmt->get_result();  
    $upline_user = $result->fetch_assoc();  
  
    if (!$upline_user) break;  
  
    $upline_id = $upline_user['id'];  
    $bonus = $amount * $percent;  
  
    // Add bonus to balance and referral_earnings  
    $stmt = $conn->prepare("UPDATE users SET balance = balance + ?, referral_earnings = referral_earnings + ? WHERE id = ?");  
    $stmt->bind_param("ddi", $bonus, $bonus, $upline_id);  
    $stmt->execute();  
  
    // Next level  
    $current_user = $upline_id;  
  }  
  
  $_SESSION['invest_message'] = "Investment successful!";  
  header("Location: vip.php");  
  exit;  
} else {  
  $_SESSION['invest_message'] = "Invalid investment data.";  
  header("Location: vip.php");
exit;
}
?>
