@page "/profile/session"
@using Indotalent.ConfigBackEnd.Interfaces
@using Indotalent.ConfigFrontEnd.Common
@using Indotalent.Infrastructure.Authentication
@using Indotalent.Infrastructure.Authentication.Identity
@using Microsoft.JSInterop
@using MudBlazor
@inject ICurrentUserService CurrentUserService
@inject TokenProvider TokenProvider
@inject IDialogService DialogService
@inject ISnackbar Snackbar
@inject IJSRuntime JSRuntime

<MudPaper Elevation="0" Outlined="true" Class="pa-8" Style="border-radius: 4px; border: 1px solid #DCEBFA;">
    <MudText Typo="Typo.h6" GutterBottom="true" Style="font-weight: 800;">Active Session Information</MudText>
    <MudText Typo="Typo.body2" Class="mb-6 text-muted">Detailed information about your current authentication state.</MudText>

    <div class="d-flex flex-column gap-1">
        <div class="pa-4" style="border-left: 4px solid var(--mud-palette-success); background: #f8fafc; border-bottom: 1px solid #eff6ff;">
            <MudText Typo="Typo.caption" Style="font-weight: 700; color: #64748b; text-transform: uppercase;">Full Name</MudText>
            <MudText Typo="Typo.body1" Style="font-weight: 600;">@(CurrentUserService.FullName ?? "Not Identified")</MudText>
        </div>

        <div class="pa-4" style="border-left: 4px solid var(--mud-palette-primary); background: #f8fafc; border-bottom: 1px solid #eff6ff;">
            <MudText Typo="Typo.caption" Style="font-weight: 700; color: #64748b; text-transform: uppercase;">User ID</MudText>
            <MudText Typo="Typo.body1" Style="font-family: monospace; font-weight: 600;">@(CurrentUserService.UserId ?? "Not Identified")</MudText>
        </div>
        
        <div class="pa-4" style="border-left: 4px solid var(--mud-palette-info); background: #f8fafc; border-bottom: 1px solid #eff6ff;">
            <MudText Typo="Typo.caption" Style="font-weight: 700; color: #64748b; text-transform: uppercase;">Username</MudText>
            <MudText Typo="Typo.body1" Style="font-weight: 600;">@(CurrentUserService.UserName ?? "Not Identified")</MudText>
        </div>

        <div class="pa-4" style="border-left: 4px solid var(--mud-palette-secondary); background: #f8fafc;">
            <MudText Typo="Typo.caption" Style="font-weight: 700; color: #64748b; text-transform: uppercase;">Email Address</MudText>
            <MudText Typo="Typo.body1" Style="font-weight: 600;">@(CurrentUserService.Email ?? "Not Identified")</MudText>
        </div>
    </div>

    <MudDivider Class="my-8" />

    <div class="d-flex align-center justify-space-between pa-6" style="background-color: #F1F5F9; border: 1px solid #E2E8F0; border-radius: 4px;">
        <div>
            <MudText Typo="Typo.subtitle1" Style="font-weight: 700;">JSON Web Token (JWT)</MudText>
            <MudText Typo="Typo.body2" Style="color: #64748b;">Copy this token to use in Swagger Authorization.</MudText>
        </div>
        <MudButton Variant="Variant.Filled"
                   Color="Color.Primary"
                   StartIcon="@Icons.Material.Filled.ContentCopy"
                   OnClick="CopyToClipboard"
                   Style="text-transform: none; font-weight: 700; border-radius: 4px; height: 48px; min-width: 180px;">
            Copy JWT Token
        </MudButton>
    </div>
</MudPaper>

@code {
    [Inject] private IHttpContextAccessor HttpContextAccessor { get; set; } = default!;

    protected override void OnInitialized()
    {
        if (string.IsNullOrEmpty(TokenProvider.Token))
        {
            var tokenFromCookie = HttpContextAccessor.HttpContext?.Request.Cookies["X-Auth-Token"];
            if (!string.IsNullOrEmpty(tokenFromCookie))
            {
                TokenProvider.Token = tokenFromCookie;
            }
        }
    }

    private async Task CopyToClipboard()
    {
        var tokenValue = TokenProvider.Token;

        if (string.IsNullOrEmpty(tokenValue))
        {
            Snackbar.Add("Token not found. Please re-login.", Severity.Error);
            return;
        }

        await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", tokenValue);
        Snackbar.Add("Token copied to clipboard!", Severity.Success);
    }

    private async Task ShowJwtDialog()
    {
        var tokenValue = TokenProvider.Token;

        if (string.IsNullOrEmpty(tokenValue))
        {
            await DialogService.ShowMessageBoxAsync("System Note", "Token is not available.");
            return;
        }

        var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Medium, FullWidth = true };

        await DialogService.ShowMessageBoxAsync(
            "Your JWT Token",
            (MarkupString)$@"<div style='background: #1e293b; color: #f8fafc; padding: 20px; border-radius: 4px; margin-top: 10px;'>                                <p style='font-size: 10px; color: #94a3b8; margin-bottom: 10px;'>RAW TOKEN:</p>                                <code style='word-break: break-all; font-size: 11px; font-family: monospace;'>{tokenValue}</code>                             </div>",
            "Close",
            options: options
        );
    }
}