<?php
require_once __DIR__ . '/includes/init.php';
require_login();

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

$success_message = '';
$error_message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['change_password'])) {
    $current_password = $_POST['current_password'];
    $new_password = $_POST['new_password'];
    $confirm_password = $_POST['confirm_password'];

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

    if ($user && password_verify($current_password, $user['password_hash'])) {
        if ($new_password === $confirm_password) {
            $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
            $stmt = $db->prepare("UPDATE users SET password_hash = ? WHERE id = ?");
            $stmt->execute([$hashed_password, $user_id]);
            $success_message = t('password_changed_successfully');
        } else {
            $error_message = t('passwords_do_not_match');
        }
    } else {
        $error_message = t('incorrect_current_password');
    }
}

// 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);
}

$page_title = t('profile_header');

require_once __DIR__ . '/includes/html_head.php';
require_once __DIR__ . '/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 ($success_message): ?>
                    <div class="alert alert-success"><?= $success_message ?></div>
                <?php endif; ?>
                <?php if ($error_message): ?>
                    <div class="alert alert-danger"><?= $error_message ?></div>
                <?php endif; ?>
                <?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('change_password') ?></h4>
                            <form method="post">
                                <div class="mb-3">
                                    <label for="current_password" class="form-label"><?= t('current_password') ?></label>
                                    <input type="password" class="form-control" id="current_password" name="current_password" required>
                                </div>
                                <div class="mb-3">
                                    <label for="new_password" class="form-label"><?= t('new_password') ?></label>
                                    <input type="password" class="form-control" id="new_password" name="new_password" required>
                                </div>
                                <div class="mb-3">
                                    <label for="confirm_password" class="form-label"><?= t('confirm_new_password') ?></label>
                                    <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
                                </div>
                                <button type="submit" name="change_password" class="btn btn-primary"><?= t('change_password_btn') ?></button>
                            </form>
                        </div>
                    </div>
                <?php else: ?>
                    <div class="alert alert-danger" role="alert">
                        <?= t('profile_error_loading') ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
<?php
require_once __DIR__ . '/includes/footer.php';
?>