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

<h2>Account Info</h2>

<div class="card" style="max-width: 600px; margin: auto; padding: 1rem; box-shadow: 0 0 10px #ccc; text-align:center;">
    @if (!string.IsNullOrEmpty(pictureUrl))
    {
        <img src="@pictureUrl" alt="Profilbild" style="width:120px; height:120px; border-radius:50%; object-fit:cover; margin-bottom:1rem;" />
    }
    else
    {
        <div style="width:120px; height:120px; border-radius:50%; background:#ccc; display:inline-block; line-height:120px; font-size:48px; color:#fff; margin-bottom:1rem;">
            <span>@(User.Identity?.Name?.Substring(0,1).ToUpper() ?? "?")</span>
        </div>
    }

    <h3>@(User.FindFirst("name")?.Value ?? "Unbekannter Nutzer")</h3>

    <table class="table" style="margin-top: 1rem;">
        <tbody>
            <tr></tr>
                <th>Username</th>
                <td>@(@User.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value ?? "Nicht verfügbar")</td>
            </tr>
            <tr>
                <th>E-Mail</th>
                <td>@(@User.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")?.Value ?? "Nicht verfügbar")</td>
            </tr>
            <tr>
                <th>Benutzer-ID</th>
                <td>@(User.FindFirst("sub")?.Value ?? "Nicht verfügbar")</td>
            </tr>
            <tr>
                <th>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>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>Rollen</th>
                <td>
                    @{
                        var roles = User.FindAll("role").Select(r => r.Value);
                        if (!roles.Any())
                        {
                            <text>Keine Rollen</text>
                        }
                        else
                        {
                            <ul>
                                @foreach (var role in roles)
                                {
                                    <li>@role</li>
                                }
                            </ul>
                        }
                    }
                </td>
            </tr>
        </tbody>
    </table>

    <form method="post" asp-controller="Auth" asp-action="Logout">
        <button type="submit" class="btn btn-danger">Abmelden</button>
    </form>
</div>


<h3>Alle Claims</h3>
<ul>
@foreach (var claim in User.Claims)
{
    <li>@claim.Type: @claim.Value</li>
}
</ul>