<?php

require_once __DIR__ . '/lib/bootstrap.php';

if (empty($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$userId = (int)$_SESSION['user_id'];

$conn = db_connect();

$stmt = $conn->prepare('SELECT userID, displayName, email, profilePicture FROM users WHERE userID = ? LIMIT 1');
if (!$stmt) {
    http_response_code(500);
    die('Datenbankfehler');
}

$stmt->bind_param('i', $userId);
$stmt->execute();

$result = $stmt->get_result();

if ($result) {
    $user = mysqli_fetch_assoc($result);
} else {
    $user = null;
}

$stmt->close();
$conn->close();

if (!$user) {
    session_unset();
    session_destroy();
    header('Location: login.php');
    exit();
}

include 'header.php';
?>

    <main class="auth" role="main">
        <section class="account" aria-label="Account Bereich">

            <?php if (isset($_GET['upload']) && $_GET['upload'] === 'ok'): ?>
                <p class="auth__alert__sucess account__toast" role="status">Profilbild wurde erfolgreich
                    aktualisiert.</p>
            <?php endif; ?>
            <?php if (isset($_GET['upload']) && $_GET['upload'] === 'err'): ?>
                <p class="auth__alert__error account__toast" role="alert">Upload fehlgeschlagen. Bitte eine gültige
                    Bilddatei auswählen.</p>
            <?php endif; ?>

            <!-- ═══ Profil-Sidebar ═══ -->
            <div class="auth__card account__profile">
                <div class="account__avatar-wrapper">
                    <img class="account__avatar"
                         src="<?php echo htmlspecialchars($user['profilePicture']); ?>"
                         alt="Profilbild von <?php echo htmlspecialchars($user['displayName'], ENT_QUOTES, 'UTF-8'); ?>"
                         width="180">
                </div>

                <h1 class="account__displayname"><?php echo htmlspecialchars($user['displayName'], ENT_QUOTES, 'UTF-8'); ?></h1>

                <dl class="account__details">
                    <div class="account__detail-row">
                        <dt>User-ID</dt>
                        <dd>#<?php echo (int)$user['userID']; ?></dd>
                    </div>
                    <div class="account__detail-row">
                        <dt>E-Mail</dt>
                        <dd><?php echo htmlspecialchars($user['email']); ?></dd>
                    </div>
                </dl>
            </div>

            <!-- ═══ Einstellungen ═══ -->
            <div class="account__settings">

                <!-- Profilbild ändern -->
                <div class="auth__card account__section">
                    <h2 class="account__section-title">
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor"
                             stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                            <rect x="3" y="3" width="18" height="18" rx="2" ry="2"/>
                            <circle cx="8.5" cy="8.5" r="1.5"/>
                            <polyline points="21 15 16 10 5 21"/>
                        </svg>
                        Profilbild ändern
                    </h2>
                    <form class="auth__form" action="upload.php" method="post" enctype="multipart/form-data">
                        <div class="auth__field">
                            <label for="file">Neue Bilddatei auswählen</label>
                            <input type="file" name="uploadFile" id="file" accept="image/*" required>
                            <p class="auth__tip">Erlaubt: JPG / PNG &middot; max. 20 MB</p>
                        </div>
                        <div class="auth__actions">
                            <button class="auth__submit" type="submit">Hochladen</button>
                        </div>
                    </form>
                </div>

                <!-- Schnellaktionen -->
                <div class="auth__card account__section">
                    <h2 class="account__section-title">
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor"
                             stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                            <path d="M12 5v14M5 12h14"/>
                        </svg>
                        Schnellaktionen
                    </h2>
                    <div class="account__quick-actions">
                        <?php if (!empty($_SESSION['user_roles']) && in_array('ADMIN', $_SESSION['user_roles'], true)): ?>
                            <a href="productAdder.php" class="auth__submit account__action-link">
                                Produkt hinzufügen
                            </a>
                            <a href="admin_users.php" class="auth__submit account__action-link" style="margin-top: 10px; background-color: #3b82f6;">
                                Benutzerverwaltung
                            </a>
                        <?php endif; ?>
                        <a href="wunschliste.php"
                           class="auth__submit account__action-link account__action-link--secondary">
                            Meine Wunschliste
                        </a>
                    </div>
                </div>

                <!-- Abmelden -->
                <div class="auth__card account__section account__section--danger">
                    <h2 class="account__section-title account__section-title--danger">
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor"
                             stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                            <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/>
                            <polyline points="16 17 21 12 16 7"/>
                            <line x1="21" y1="12" x2="9" y2="12"/>
                        </svg>
                        Sitzung beenden
                    </h2>
                    <p class="account__danger-text">Du wirst ausgeloggt und musst dich erneut anmelden.</p>
                    <form action="logout.php" method="post">
                        <div class="auth__actions">
                            <button class="auth__submit auth__submit--danger" type="submit">Abmelden</button>
                        </div>
                    </form>
                </div>

            </div>
        </section>
    </main>

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