<?php
session_start();
require_once 'includes/auth.php';
require_once 'db/config.php';
require_once 'includes/i18n.php';
require_once 'includes/helpers.php';

if (!is_logged_in()) {
    header('Location: login.php');
    exit;
}

$user_id = $_SESSION['user_id'];
$db = db();

// Fetch user data
$stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

// Fetch client data if client_id is available
$client = null;
if (!empty($user['client_id'])) {
    $stmt = $db->prepare("SELECT * FROM clients WHERE id = ?");
    $stmt->execute([$user['client_id']]);
    $client = $stmt->fetch(PDO::FETCH_ASSOC);
}
?>
<!DOCTYPE html>
<html lang="<?= get_lang() ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= t('profile_user_profile') ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link href="assets/css/custom.css" rel="stylesheet">
</head>
<body>
    <?php require_once 'includes/header.php'; ?>
    <div class="container mt-5">
        <div class="card">
            <div class="card-header">
                <h3><?= t('profile_user_profile') ?></h3>
            </div>
            <div class="card-body">
                <?php if ($user): ?>
                    <div class="row">
                        <div class="col-md-6">
                            <h4><?= t('profile_welcome') ?>, <?php echo htmlspecialchars($user['username']); ?></h4>
                            <ul class="list-group list-group-flush">
                                <li class="list-group-item">
                                    <i class="fas fa-envelope"></i>
                                    <strong><?= t('profile_email_address') ?>:</strong> <?php echo htmlspecialchars($user['email']); ?>
                                </li>
                                <?php if ($client): ?>
                                    <li class="list-group-item">
                                        <i class="fas fa-building"></i>
                                        <strong><?= t('profile_client') ?>:</strong> <?php echo htmlspecialchars($client['name']); ?>
                                    </li>
                                <?php endif; ?>
                            </ul>
                        </div>
                        <div class="col-md-6">
                            <h4><?= t('profile_password_management') ?></h4>
                            <p><?= t('profile_feature_in_preparation') ?></p>
                        </div>
                    </div>
                <?php else: ?>
                    <div class="alert alert-danger" role="alert">
                        <?= t('profile_error_loading') ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
