<?php
require_once 'config.php';
require_once 'classes/Security.php';
require_once 'classes/User.php';

Security::requireAuth();

$pageTitle = 'Profile Settings - CryptoExchange Pro | Account Management';
$pageDescription = 'Manage your account settings, personal information, and preferences on CryptoExchange Pro.';

$user = new User();
$userInfo = $user->getUserById($_SESSION['user_id']);

$message = '';
$messageType = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!Security::validateCSRFToken($_POST['csrf_token'])) {
        $message = 'Invalid security token. Please try again.';
        $messageType = 'danger';
    } else {
        $result = $user->updateProfile($_SESSION['user_id'], $_POST);
        $message = $result['message'];
        $messageType = $result['success'] ? 'success' : 'danger';
        
        if ($result['success']) {
            // Refresh user info
            $userInfo = $user->getUserById($_SESSION['user_id']);
        }
    }
}

include 'includes/header.php';
?>

<main style="min-height: 100vh; background-color: var(--bg-secondary); padding: 40px 0;">
    <div class="container">
        <!-- Header -->
        <div class="mb-4">
            <h1 style="font-size: 2.5rem; font-weight: 700; color: var(--text-primary); margin-bottom: 8px;">
                <i class="fas fa-user-cog" style="color: var(--primary-color); margin-right: 16px;"></i>Profile Settings
            </h1>
            <p style="color: var(--text-secondary); font-size: 16px;">Manage your account information and preferences</p>
        </div>

        <!-- Alert Messages -->
        <?php if ($message): ?>
        <div class="alert alert-<?php echo $messageType; ?> fade-in mb-4">
            <div class="d-flex align-items-center">
                <div style="margin-right: 12px;">
                    <i class="fas <?php echo $messageType === 'success' ? 'fa-check-circle' : 'fa-exclamation-triangle'; ?>"></i>
                </div>
                <div>
                    <p style="margin: 0; font-weight: 500;"><?php echo $message; ?></p>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <div class="row">
            <!-- Profile Form -->
            <div class="col-8">
                <div class="card">
                    <div class="card-header">
                        <h2 style="margin: 0; font-size: 1.25rem; font-weight: 600;">
                            <i class="fas fa-user" style="color: var(--primary-color); margin-right: 8px;"></i>
                            Personal Information
                        </h2>
                    </div>
                    <div class="card-body">
                        <form method="POST" action="">
                            <input type="hidden" name="csrf_token" value="<?php echo Security::generateCSRFToken(); ?>">
                            
                            <div class="row">
                                <div class="col-6">
                                    <div class="form-group">
                                        <label class="form-label">First Name *</label>
                                        <input type="text" name="first_name" class="form-control" required 
                                               value="<?php echo htmlspecialchars($userInfo['first_name'] ?? ''); ?>">
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="form-group">
                                        <label class="form-label">Last Name *</label>
                                        <input type="text" name="last_name" class="form-control" required
                                               value="<?php echo htmlspecialchars($userInfo['last_name'] ?? ''); ?>">
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-6">
                                    <div class="form-group">
                                        <label class="form-label">Email Address</label>
                                        <input type="email" class="form-control" value="<?php echo htmlspecialchars($userInfo['email']); ?>" disabled>
                                        <small style="color: var(--text-secondary); font-size: 12px;">Email cannot be changed. Contact support if needed.</small>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="form-group">
                                        <label class="form-label">Username</label>
                                        <input type="text" class="form-control" value="<?php echo htmlspecialchars($userInfo['username']); ?>" disabled>
                                        <small style="color: var(--text-secondary); font-size: 12px;">Username cannot be changed.</small>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-6">
                                    <div class="form-group">
                                        <label class="form-label">Phone Number</label>
                                        <input type="tel" name="phone" class="form-control" 
                                               value="<?php echo htmlspecialchars($userInfo['phone'] ?? ''); ?>"
                                               placeholder="+1234567890">
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="form-group">
                                        <label class="form-label">Country</label>
                                        <select name="country" class="form-control form-select">
                                            <option value="">Select Country</option>
                                            <option value="US" <?php echo ($userInfo['country'] ?? '') === 'US' ? 'selected' : ''; ?>>United States</option>
                                            <option value="GB" <?php echo ($userInfo['country'] ?? '') === 'GB' ? 'selected' : ''; ?>>United Kingdom</option>
                                            <option value="CA" <?php echo ($userInfo['country'] ?? '') === 'CA' ? 'selected' : ''; ?>>Canada</option>
                                            <option value="AU" <?php echo ($userInfo['country'] ?? '') === 'AU' ? 'selected' : ''; ?>>Australia</option>
                                            <option value="DE" <?php echo ($userInfo['country'] ?? '') === 'DE' ? 'selected' : ''; ?>>Germany</option>
                                            <option value="FR" <?php echo ($userInfo['country'] ?? '') === 'FR' ? 'selected' : ''; ?>>France</option>
                                            <option value="JP" <?php echo ($userInfo['country'] ?? '') === 'JP' ? 'selected' : ''; ?>>Japan</option>
                                            <option value="KR" <?php echo ($userInfo['country'] ?? '') === 'KR' ? 'selected' : ''; ?>>South Korea</option>
                                            <option value="SG" <?php echo ($userInfo['country'] ?? '') === 'SG' ? 'selected' : ''; ?>>Singapore</option>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="form-label">Date of Birth</label>
                                <input type="date" name="date_of_birth" class="form-control" 
                                       value="<?php echo $userInfo['date_of_birth'] ?? ''; ?>">
                            </div>

                            <div class="form-group">
                                <label class="form-label">Address</label>
                                <textarea name="address" class="form-control" rows="3" 
                                          placeholder="Enter your full address"><?php echo htmlspecialchars($userInfo['address'] ?? ''); ?></textarea>
                            </div>

                            <div class="d-flex justify-content-between align-items-center">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-save"></i> Save Changes
                                </button>
                                <a href="security.php" class="btn btn-outline-primary">
                                    <i class="fas fa-shield-alt"></i> Security Settings
                                </a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

            <!-- Account Info -->
            <div class="col-4">
                <div class="card mb-4">
                    <div class="card-header">
                        <h3 style="margin: 0; font-size: 1.25rem; font-weight: 600;">
                            <i class="fas fa-info-circle" style="color: var(--primary-color); margin-right: 8px;"></i>
                            Account Status
                        </h3>
                    </div>
                    <div class="card-body">
                        <div style="display: flex; flex-direction: column; gap: 16px;">
                            <div class="d-flex justify-content-between align-items-center">
                                <span style="color: var(--text-secondary);">Account Type:</span>
                                <span class="badge badge-primary">
                                    <?php echo $userInfo['is_admin'] ? 'Administrator' : 'Standard'; ?>
                                </span>
                            </div>
                            
                            <div class="d-flex justify-content-between align-items-center">
                                <span style="color: var(--text-secondary);">KYC Status:</span>
                                <?php
                                $kycStatusColors = [
                                    'pending' => 'badge-warning',
                                    'verified' => 'badge-success',
                                    'rejected' => 'badge-danger'
                                ];
                                ?>
                                <span class="badge <?php echo $kycStatusColors[$userInfo['kyc_status']] ?? 'badge-warning'; ?>">
                                    <?php echo strtoupper($userInfo['kyc_status']); ?>
                                </span>
                            </div>
                            
                            <div class="d-flex justify-content-between align-items-center">
                                <span style="color: var(--text-secondary);">KYC Level:</span>
                                <span style="font-weight: 600; color: var(--text-primary);">
                                    Level <?php echo $userInfo['kyc_level']; ?>
                                </span>
                            </div>
                            
                            <div class="d-flex justify-content-between align-items-center">
                                <span style="color: var(--text-secondary);">Email Verified:</span>
                                <span class="badge <?php echo $userInfo['email_verified'] ? 'badge-success' : 'badge-warning'; ?>">
                                    <?php echo $userInfo['email_verified'] ? 'Verified' : 'Pending'; ?>
                                </span>
                            </div>
                            
                            <div class="d-flex justify-content-between align-items-center">
                                <span style="color: var(--text-secondary);">2FA Enabled:</span>
                                <span class="badge <?php echo $userInfo['two_factor_enabled'] ? 'badge-success' : 'badge-warning'; ?>">
                                    <?php echo $userInfo['two_factor_enabled'] ? 'Enabled' : 'Disabled'; ?>
                                </span>
                            </div>
                            
                            <div class="d-flex justify-content-between align-items-center">
                                <span style="color: var(--text-secondary);">Member Since:</span>
                                <span style="font-weight: 500; color: var(--text-primary);">
                                    <?php echo date('M Y', strtotime($userInfo['created_at'])); ?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card mb-4">
                    <div class="card-header">
                        <h3 style="margin: 0; font-size: 1.25rem; font-weight: 600;">
                            <i class="fas fa-chart-bar" style="color: var(--secondary-color); margin-right: 8px;"></i>
                            Trading Limits
                        </h3>
                    </div>
                    <div class="card-body">
                        <div style="display: flex; flex-direction: column; gap: 16px;">
                            <?php
                            $limits = [
                                0 => ['daily' => 0, 'monthly' => 0],
                                1 => ['daily' => 1000, 'monthly' => 10000],
                                2 => ['daily' => 10000, 'monthly' => 100000]
                            ];
                            $userLimits = $limits[$userInfo['kyc_level']] ?? $limits[0];
                            ?>
                            
                            <div class="d-flex justify-content-between align-items-center">
                                <span style="color: var(--text-secondary);">Daily Limit:</span>
                                <span style="font-weight: 600; color: var(--text-primary);">
                                    $<?php echo number_format($userLimits['daily']); ?>
                                </span>
                            </div>
                            
                            <div class="d-flex justify-content-between align-items-center">
                                <span style="color: var(--text-secondary);">Monthly Limit:</span>
                                <span style="font-weight: 600; color: var(--text-primary);">
                                    $<?php echo number_format($userLimits['monthly']); ?>
                                </span>
                            </div>
                            
                            <?php if ($userInfo['kyc_level'] < 2): ?>
                            <div style="padding: 12px; background: rgba(26, 115, 232, 0.1); border-radius: var(--border-radius); border-left: 4px solid var(--primary-color);">
                                <p style="margin: 0; font-size: 14px; color: var(--primary-color);">
                                    <i class="fas fa-info-circle"></i>
                                    Complete KYC verification to increase your trading limits.
                                </p>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <div class="card">
                    <div class="card-header">
                        <h3 style="margin: 0; font-size: 1.25rem; font-weight: 600;">
                            <i class="fas fa-cog" style="color: var(--warning-color); margin-right: 8px;"></i>
                            Quick Actions
                        </h3>
                    </div>
                    <div class="card-body">
                        <div style="display: flex; flex-direction: column; gap: 12px;">
                            <a href="kyc.php" class="btn btn-outline-primary btn-sm">
                                <i class="fas fa-id-card"></i> KYC Verification
                            </a>
                            <a href="security.php" class="btn btn-outline-primary btn-sm">
                                <i class="fas fa-shield-alt"></i> Security Settings
                            </a>
                            <a href="transactions.php" class="btn btn-outline-primary btn-sm">
                                <i class="fas fa-history"></i> Transaction History
                            </a>
                            <a href="support.php" class="btn btn-outline-primary btn-sm">
                                <i class="fas fa-headset"></i> Contact Support
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

<?php include 'includes/footer.php'; ?>