<?php
// ==========================
// profile.php - DemoBank
// ==========================

require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';

if (session_status() === PHP_SESSION_NONE) session_start();

// Ensure user is logged in
$user_id = $_SESSION['user_id'] ?? null;
if (!$user_id) {
    header("Location: /auth/login.php");
    exit;
}

// Database connection
$conn = db();
if (!$conn) die("Database connection failed");

// Fetch user info
$stmt = $conn->prepare("SELECT * FROM users WHERE id=?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();

// Fetch user accounts
$stmt = $conn->prepare("SELECT id, account_number, balance, currency, account_type FROM accounts WHERE user_id=?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$accounts = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

// Currency symbol map
$currencySymbols = [
    'USD' => '$',
    'EUR' => '€',
    'GBP' => '£',
    'JPY' => '¥',
    'CNY' => '¥',
    'NGN' => '₦',
    'GHS' => '₵',
    'INR' => '₹',
    'CAD' => 'C$',
    'AUD' => 'A$'
];

// Capture content for layout
ob_start();
?>

<h1>My Profile</h1>

<div style="display:flex;gap:30px;align-items:flex-start;flex-wrap:wrap;">

    <!-- PROFILE PHOTO -->
    <div>
        <?php if (!empty($user['profile_photo'])): ?>
            <img src="<?= htmlspecialchars($user['profile_photo']); ?>" 
                 alt="Profile Photo"
                 style="width:120px;height:120px;border-radius:50%;object-fit:cover;">
        <?php else: ?>
            <div style="width:120px;height:120px;border-radius:50%;background:#ccc;display:flex;align-items:center;justify-content:center;">
                No Photo
            </div>
        <?php endif; ?>
    </div>

    <!-- USER INFO -->
    <div style="flex:1;min-width:250px;">
        <p><strong>Full Name:</strong> <?= htmlspecialchars($user['first_name'] . ' ' . $user['last_name']); ?></p>
        <p><strong>Email:</strong> <?= htmlspecialchars($user['email']); ?></p>
        <p><strong>Currency:</strong> <?= htmlspecialchars(($currencySymbols[$user['currency']] ?? $user['currency']) . " ({$user['currency']})"); ?></p>
        <p><strong>Status:</strong> 
            <span style="color:<?= ($user['status'] ?? '') === 'active' ? 'green' : 'red'; ?>">
                <?= ucfirst($user['status'] ?? 'unknown'); ?>
            </span>
        </p>
    </div>

</div>

<!-- ADDRESS SECTION -->
<div style="margin-top:30px;">
    <h2>Address Information</h2>
    <p><strong>Address:</strong> <?= htmlspecialchars($user['address'] ?? 'Not set'); ?></p>
    <p><strong>City:</strong> <?= htmlspecialchars($user['city'] ?? 'Not set'); ?></p>
    <p><strong>State:</strong> <?= htmlspecialchars($user['state'] ?? 'Not set'); ?></p>
    <p><strong>Zip Code:</strong> <?= htmlspecialchars($user['zip_code'] ?? 'Not set'); ?></p>
    <p><strong>Country:</strong> <?= htmlspecialchars($user['country'] ?? 'Not set'); ?></p>
</div>

<!-- USER ACCOUNTS -->
<div style="margin-top:30px;">
    <h2>Accounts</h2>
    <div style="display:flex;gap:20px;flex-wrap:wrap;">
        <?php foreach($accounts as $acc): 
            $symbol = $currencySymbols[$acc['currency']] ?? $acc['currency'];
        ?>
            <div style="flex:1;min-width:200px;background:#fff;padding:20px;border-radius:8px;box-shadow:0 2px 5px rgba(0,0,0,0.1);">
                <h4>Account <?= htmlspecialchars($acc['account_number']) ?></h4>
                <p>Type: <?= htmlspecialchars($acc['account_type'] ?? 'Standard') ?></p>
                <p>Balance: <?= $symbol . number_format($acc['balance'],2) ?> <?= htmlspecialchars($acc['currency']) ?></p>
            </div>
         <?php endforeach; ?>
        </div>
    </div>

    <!-- QUICK ACTIONS -->
    <div style="margin-top:30px;display:flex;gap:15px;flex-wrap:wrap;">
        <a href="/user/edit_profile.php" class="btn" style="padding:10px 20px;background:#1a73e8;color:white;text-decoration:none;border-radius:5px;">Edit Profile</a>
        <a href="/user/notifications.php" class="btn" style="padding:10px 20px;background:#fbbc05;color:white;text-decoration:none;border-radius:5px;">Notifications</a>
    </div>

</div>

<?php
$content = ob_get_clean();

// Render layout
renderLayout("Profile", $content, $conn);