<?php
require_once dirname(__DIR__) . '/config/config.php';
requireLogin();

$auth = new Auth();
$userModel = new User();
$currentBalance = $auth->refreshBalance($_SESSION['user_id']);
$user = $userModel->getById($_SESSION['user_id']);

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && verifyCSRFToken($_POST['csrf_token'] ?? '')) {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'update_profile') {
        $name = trim($_POST['name'] ?? '');
        $email = trim($_POST['email'] ?? '');
        
        if (empty($name) || strlen($name) < 2) {
            $error = 'Name must be at least 2 characters';
        } elseif (!isValidEmail($email)) {
            $error = 'Invalid email address';
        } elseif ($userModel->emailExists($email, $_SESSION['user_id'])) {
            $error = 'Email already in use';
        } else {
            $userModel->update($_SESSION['user_id'], ['name' => $name, 'email' => $email]);
            $_SESSION['name'] = $name;
            $_SESSION['email'] = $email;
            $success = 'Profile updated successfully';
            $user = $userModel->getById($_SESSION['user_id']);
        }
    } elseif ($action === 'change_password') {
        $currentPassword = $_POST['current_password'] ?? '';
        $newPassword = $_POST['new_password'] ?? '';
        $confirmPassword = $_POST['confirm_password'] ?? '';
        
        if (!verifyPassword($currentPassword, $user['password'])) {
            $error = 'Current password is incorrect';
        } elseif ($newPassword !== $confirmPassword) {
            $error = 'New passwords do not match';
        } else {
            $passwordCheck = isStrongPassword($newPassword);
            if (!$passwordCheck['valid']) {
                $error = $passwordCheck['message'];
            } else {
                $userModel->updatePassword($_SESSION['user_id'], $newPassword);
                $success = 'Password changed successfully';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profile - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Segoe UI', sans-serif; background: #0A1628; color: #fff; min-height: 100vh; }
        .header { background: #0F2744; padding: 1rem; display: flex; justify-content: space-between; align-items: center; position: sticky; top: 0; z-index: 100; }
        .header-logo { display: flex; align-items: center; gap: 0.5rem; color: #F5A623; font-weight: 700; }
        .header-logo img { width: 48px; height: 48px; min-width: 48px; min-height: 48px; object-fit: contain; }
        .header-balance { background: #F5A623; color: #0A1628; padding: 0.5rem 1rem; border-radius: 20px; font-weight: 600; }
        .hamburger { background: none; border: none; color: #fff; font-size: 1.5rem; cursor: pointer; }
        .menu-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 200; display: none; }
        .menu-overlay.active { display: block; }
        .side-menu { position: fixed; top: 0; left: -280px; width: 280px; height: 100%; background: #0F2744; z-index: 300; transition: left 0.3s; padding: 1.5rem 0; overflow-y: auto; }
        .side-menu.active { left: 0; }
        .menu-header { display: flex; justify-content: space-between; align-items: center; padding: 0 1.5rem 1.5rem; border-bottom: 1px solid #1E3A5F; }
        .menu-close { background: none; border: none; color: #9CA3AF; font-size: 1.5rem; cursor: pointer; }
        .menu-items { padding: 1rem 0; }
        .menu-item { display: flex; align-items: center; gap: 1rem; padding: 1rem 1.5rem; color: #E5E7EB; text-decoration: none; }
        .menu-item:hover, .menu-item.active { background: #1E3A5F; color: #F5A623; }
        .menu-item i { width: 20px; text-align: center; }
        .menu-divider { height: 1px; background: #1E3A5F; margin: 0.5rem 1.5rem; }
        .content { padding: 1rem; max-width: 600px; margin: 0 auto; }
        .page-title { font-size: 1.25rem; margin-bottom: 1rem; }
        .page-title i { color: #F5A623; }
        .card { background: #0F2744; border-radius: 12px; padding: 1.5rem; margin-bottom: 1rem; }
        .card-title { font-size: 1rem; color: #F5A623; margin-bottom: 1rem; display: flex; align-items: center; gap: 0.5rem; }
        .form-group { margin-bottom: 1rem; }
        .form-label { display: block; color: #E5E7EB; margin-bottom: 0.5rem; font-size: 0.9rem; }
        .form-control { width: 100%; padding: 0.75rem 1rem; background: #0A1628; border: 1px solid #1E3A5F; border-radius: 8px; color: #fff; font-size: 1rem; }
        .form-control:focus { outline: none; border-color: #F5A623; }
        .btn { display: inline-flex; align-items: center; gap: 0.5rem; padding: 0.75rem 1.5rem; border: none; border-radius: 8px; font-size: 1rem; font-weight: 600; cursor: pointer; }
        .btn-gold { background: #F5A623; color: #0A1628; width: 100%; justify-content: center; }
        .alert { padding: 0.75rem 1rem; border-radius: 8px; margin-bottom: 1rem; }
        .alert-success { background: rgba(16,185,129,0.2); border: 1px solid #10B981; color: #10B981; }
        .alert-danger { background: rgba(239,68,68,0.2); border: 1px solid #EF4444; color: #EF4444; }
        .user-card { background: linear-gradient(135deg, #1E3A5F 0%, #0F2744 100%); border: 1px solid #F5A623; text-align: center; }
        .user-avatar { width: 80px; height: 80px; background: #0A1628; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 2rem; color: #F5A623; margin: 0 auto 1rem; border: 3px solid #F5A623; }
        .user-name { font-size: 1.25rem; font-weight: 600; color: #fff; margin-bottom: 0.25rem; }
        .user-email { color: #9CA3AF; font-size: 0.9rem; margin-bottom: 0.5rem; }
        .user-since { color: #9CA3AF; font-size: 0.8rem; }
        .user-balance { font-size: 1.5rem; font-weight: 700; color: #F5A623; margin-top: 1rem; }
    </style>
    <link rel="stylesheet" href="/assets/css/premium-dark.css">
</head>
<body>
    <header class="header">
        <button class="hamburger" onclick="toggleMenu()"><i class="fas fa-bars"></i></button>
        <div class="header-logo"><img src="/assets/images/logo.png" alt="<?php echo SITE_NAME; ?>"></div>
        <div class="header-balance"><?php echo formatCurrency($currentBalance); ?></div>
    </header>
    <div class="menu-overlay" id="menuOverlay" onclick="toggleMenu()"></div>
    <nav class="side-menu" id="sideMenu">
        <div class="menu-header">
            <div class="header-logo"><img src="/assets/images/logo.png" alt="<?php echo SITE_NAME; ?>"></div>
            <button class="menu-close" onclick="toggleMenu()"><i class="fas fa-times"></i></button>
        </div>
        <div class="menu-items">
            <a href="dashboard.php" class="menu-item"><i class="fas fa-home"></i> Dashboard</a>
            <a href="sms.php" class="menu-item"><i class="fas fa-sms"></i> SMS Service</a>
            <a href="smm.php" class="menu-item"><i class="fas fa-rocket"></i> SMM Service</a>
            <a href="banner.php" class="menu-item"><i class="fas fa-ad"></i> Banner Service</a>
            <a href="orders.php" class="menu-item"><i class="fas fa-history"></i> Orders</a>
            <a href="add-funds.php" class="menu-item"><i class="fas fa-wallet"></i> Add Funds</a>
            <a href="transactions.php" class="menu-item"><i class="fas fa-exchange-alt"></i> Transactions</a>
            <a href="profile.php" class="menu-item active"><i class="fas fa-user"></i> Profile</a>
            <div class="menu-divider"></div>
            <a href="<?php echo SITE_URL; ?>/logout.php" class="menu-item"><i class="fas fa-sign-out-alt"></i> Log Out</a>
        </div>
    </nav>
    <main class="content">
        <h1 class="page-title"><i class="fas fa-user"></i> My Profile</h1>
        
        <?php if ($error): ?><div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div><?php endif; ?>
        <?php if ($success): ?><div class="alert alert-success"><?php echo htmlspecialchars($success); ?></div><?php endif; ?>
        
        <div class="card user-card">
            <div class="user-avatar"><i class="fas fa-user"></i></div>
            <div class="user-name"><?php echo htmlspecialchars($user['name']); ?></div>
            <div class="user-email"><?php echo htmlspecialchars($user['email']); ?></div>
            <div class="user-since">Member since <?php echo formatDate($user['created_at']); ?></div>
            <div class="user-balance"><?php echo formatCurrency($currentBalance); ?></div>
        </div>
        
        <div class="card">
            <h3 class="card-title"><i class="fas fa-edit"></i> Update Profile</h3>
            <form method="POST">
                <?php echo csrfTokenField(); ?>
                <input type="hidden" name="action" value="update_profile">
                <div class="form-group">
                    <label class="form-label">Full Name</label>
                    <input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($user['name']); ?>" required minlength="2">
                </div>
                <div class="form-group">
                    <label class="form-label">Email Address</label>
                    <input type="email" name="email" class="form-control" value="<?php echo htmlspecialchars($user['email']); ?>" required>
                </div>
                <button type="submit" class="btn btn-gold"><i class="fas fa-save"></i> Update Profile</button>
            </form>
        </div>
        
        <div class="card">
            <h3 class="card-title"><i class="fas fa-lock"></i> Change Password</h3>
            <form method="POST">
                <?php echo csrfTokenField(); ?>
                <input type="hidden" name="action" value="change_password">
                <div class="form-group">
                    <label class="form-label">Current Password</label>
                    <input type="password" name="current_password" class="form-control" required>
                </div>
                <div class="form-group">
                    <label class="form-label">New Password</label>
                    <input type="password" name="new_password" class="form-control" required minlength="8">
                    <small style="color:#9CA3AF;font-size:0.8rem;">Min 8 characters with uppercase, lowercase and number</small>
                </div>
                <div class="form-group">
                    <label class="form-label">Confirm New Password</label>
                    <input type="password" name="confirm_password" class="form-control" required>
                </div>
                <button type="submit" class="btn btn-gold"><i class="fas fa-key"></i> Change Password</button>
            </form>
        </div>
    </main>
    <script>function toggleMenu() { document.getElementById('sideMenu').classList.toggle('active'); document.getElementById('menuOverlay').classList.toggle('active'); }</script>
</body>
</html>
