@{
    ViewData["Title"] = "Account Info";
    var pictureUrl = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value ?? "";
    var Id = ViewBag.Id;
    var preferredUsername = ViewBag.name;
    var IdProvider = ViewBag.IdProvider;
}

<div class="container mt-5">
    <div class="card shadow-lg rounded-3 p-4" style="max-width: 700px; margin: auto;">
        <div class="text-center mb-4">
            @if (!string.IsNullOrEmpty(pictureUrl))
            {
                <img src="@pictureUrl" alt="Profilbild" class="rounded-circle shadow" style="width: 120px; height: 120px; object-fit: cover;" />
            }
            else
            {
                <div class="bg-secondary text-white rounded-circle d-inline-flex align-items-center justify-content-center"
                     style="width: 120px; height: 120px; font-size: 48px;">
                    <span>@(User.Identity?.Name?.Substring(0,1).ToUpper() ?? "?")</span>
                </div>
            }
            <h3 class="mt-3">
                <i class="bi bi-person-circle me-1"></i>@preferredUsername
            </h3>
            
        </div>

        <table class="table table-hover">
            <tbody>
                <tr>
                    <th><i class="bi bi-person-badge me-1"></i>Username</th>
                    <td>@preferredUsername</td>
                </tr>
                <tr>
                    <th><i class="bi bi-envelope me-1"></i>E-Mail</th>
                    <td>@(ViewBag.Mail ?? "Nicht verfügbar")</td>
                </tr>
                <tr>
                    <th><i class="bi bi-fingerprint me-1"></i>Benutzer-ID</th>
                    <td>@(ViewBag.Id ?? "Nicht verfügbar")</td>
                </tr>
                @if(IdProvider != "local")
                {
                <tr>
                    <th><i class="bi bi-clock-history me-1"></i>Login-Zeit</th>
                    <td>
                        @(User.FindFirst("iat") != null
                            ? DateTimeOffset.FromUnixTimeSeconds(long.Parse(User.FindFirst("iat").Value)).ToLocalTime().ToString()
                            : "Nicht verfügbar")
                    </td>
                </tr>
                <tr>
                    <th><i class="bi bi-hourglass-split me-1"></i>Token läuft ab</th>
                    <td>
                        @(User.FindFirst("exp") != null
                            ? DateTimeOffset.FromUnixTimeSeconds(long.Parse(User.FindFirst("exp").Value)).ToLocalTime().ToString()
                            : "Nicht verfügbar")
                    </td>
                </tr>
                <tr>
                    <th><i class="bi bi-shield-lock me-1"></i>Rollen</th>
                    <td>
                        @{
                            var roles = User.FindAll("role").Select(r => r.Value);
                            if (!roles.Any())
                            {
                                <span class="text-muted">Keine Rollen</span>
                            }
                            else
                            {
                                <ul class="mb-0">
                                    @foreach (var role in roles)
                                    {
                                        <li><i class="bi bi-tag me-1 text-primary"></i>@role</li>
                                    }
                                </ul>
                            }
                        }
                    </td>
                </tr>
                }
                
            </tbody>
        </table>
        <div>
            <form method="get" asp-controller="User" asp-action="UserSettings" class="text-center mt-4">
                <button type="submit" class="btn btn-info">
                    <i class="bi bi-gear-wide-connected me-1"></i>Einstellungen
                </button>
            </form>
            <form method="post" asp-controller="Auth" asp-action="Logout" class="text-center mt-4">
                <button type="submit" class="btn btn-danger">
                    <i class="bi bi-box-arrow-right me-1"></i>Abmelden
                </button>
            </form>
        </div>
    </div>
</div>
