<?php
session_start();
require_once '../connection.php';

// Check if admin is logged in
if (!isset($_SESSION['admin_id'])) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Not authorized']);
    exit();
}

$investment_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($investment_id > 0) {
    $query = "
        SELECT ui.*, u.username, u.email, u.full_name, u.phone, u.country, u.account_status,
               ip.name as plan_name, ip.interest_rate, ip.duration_days, 
               ip.compounding_interval, ip.description as plan_description,
               ip.min_amount, ip.max_amount
        FROM user_investments ui
        JOIN users u ON ui.user_id = u.id
        JOIN investment_plans ip ON ui.plan_id = ip.id
        WHERE ui.id = ?
    ";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("i", $investment_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        $investment = $result->fetch_assoc();
        echo json_encode(['success' => true, 'data' => $investment]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Investment not found']);
    }
    $stmt->close();
} else {
    echo json_encode(['success' => false, 'message' => 'Invalid investment ID']);
}
?>