@page "/account/logout"
@layout AuthenticationLayout

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

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

<div class="logout-container">
    <div class="logout-icon-wrapper">
        <div class="logout-icon-circle">
            <svg fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <path stroke-linecap="round" stroke-linejoin="round" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"/>
            </svg>
        </div>
    </div>

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

    <MudButton Variant="Variant.Filled"
               Size="Size.Large"
               FullWidth="true"
               Disabled="@isLoggingOut"
               OnClick="Logout"
               Style="text-transform:none; border-radius: 8px; 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">Signing out...</span>
        }
        else
        {
            <span>Sign Out</span>
        }
    </MudButton>

    <MudButton Variant="Variant.Text"
               Class="mt-3"
               FullWidth="true"
               Href="/account/tenant-selection"
               Disabled="@isLoggingOut"
               Style="text-transform:none; color: #94A3B8; font-weight: 600; border-radius: 8px;">
        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>