@page "/account/logout"
@layout AuthenticationLayout

@using Indotalent.Shared.Consts
@using Microsoft.JSInterop
@inject IJSRuntime JSRuntime

<PageTitle>Sign Out - @GlobalConsts.AppInitial</PageTitle>

<style>
    /* ===== LOGOUT CONTAINER ===== */
    .logout-container {
        display: flex;
        flex-direction: column;
        align-items: center;
        text-align: center;
    }

    .logout-icon-wrapper {
        display: flex;
        justify-content: center;
        width: 100%;
        margin-bottom: 1.5rem;
    }

    .logout-icon-circle {
        width: 5rem;
        height: 5rem;
        border-radius: 50%;
        background: #f5f3ff;
        border: 4px solid #e8e5ff;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
    }

    .logout-icon-circle::before {
        content: '';
        position: absolute;
        inset: -8px;
        border-radius: 50%;
        border: 2px solid #ddd6fe;
        opacity: 0.4;
    }

    .logout-title {
        font-size: 1.25rem;
        font-weight: 700;
        color: #0f172a;
        margin-bottom: 0.5rem;
        width: 100%;
        text-align: center;
    }

    .logout-desc {
        font-size: 0.875rem;
        color: #64748b;
        margin-bottom: 2rem;
        width: 100%;
        text-align: center;
    }

    /* ===== MISC ===== */
    .w-100 { width: 100%; }
    .mt-4 { margin-top: 1rem; }
</style>

<div class="logout-container">

    <div class="logout-icon-wrapper">
        <div class="logout-icon-circle">
            <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="#6366F1" 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>
        </div>
    </div>

    <h2 class="logout-title">Sign Out</h2>
    <p class="logout-desc">Are you sure you want to sign out of your account?</p>

    <MudButton Variant="Variant.Filled"
               Size="Size.Large"
               FullWidth="true"
               Disabled="@isLoggingOut"
               OnClick="Logout"
               Style="text-transform:none; border-radius: 0.5rem; background-color: #6366F1; color: white; height: 48px; font-weight: 600;">
        @if (isLoggingOut)
        {
            <MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" Color="Color.Inherit" />
            <span class="ms-2">Processing...</span>
        }
        else
        {
            <span>Sign Out</span>
        }
    </MudButton>

    <MudButton Variant="Variant.Text"
               Class="mt-4"
               FullWidth="true"
               Href="/"
               Disabled="@isLoggingOut"
               Style="text-transform:none; color: #94A3B8; font-weight: 600;">
        Cancel
    </MudButton>

</div>

@code {
    private bool isLoggingOut;

    private async Task Logout()
    {
        isLoggingOut = true;
        StateHasChanged();

        var result = await JSRuntime.InvokeAsync<ApiJSRuntimeResponse>("apiAccountLogout");

        if (result.Status == 200)
        {
            Snackbar.Add("Successfully signed out!", Severity.Success);
            await Task.Delay(500);
            Navigation.NavigateTo("/", forceLoad: true);
        }
        else
        {
            Snackbar.Add(result.Title ?? "Failed to logout.", Severity.Error);
            await Task.Delay(500);
        }

        isLoggingOut = false;
        StateHasChanged();
    }

    private class ApiJSRuntimeResponse
    {
        public int? Status { get; set; }
        public string? Title { get; set; }
    }
}

<script>
    window.apiAccountLogout = async () => {
        try {
            const response = await fetch('/api/account/signout', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                credentials: 'include'
            });
            if (!response.ok) {
                throw new Error('Unknown error occurred');
            }
            return { status: 200, title: 'Success user logout' };
        } catch (error) {
            return { status: 500, title: 'Server / Network error' };
        }
    };
</script>