@page "/recent-usage-stats"
@using AliasVault.Admin.Main.Models
@using AliasVault.Admin.Services
@using AliasVault.Admin.Main.Pages.Dashboard.RecentUsage.Components
@inherits MainBase
@inject StatisticsService StatisticsService

<LayoutPageTitle>Recent Usage Statistics</LayoutPageTitle>

<PageHeader
    BreadcrumbItems="@BreadcrumbItems"
    Title="Recent Usage Statistics"
    Description="View usage patterns from the last 72 hours.">
    <CustomActions>
        <RefreshButton OnClick="RefreshData" ButtonText="Refresh" />
    </CustomActions>
</PageHeader>

<div class="px-4 space-y-6">
    <!-- Recent Usage Analysis ---->
    <div class="space-y-6">
        <!-- Top Users by Aliases Created ---->
        <RecentUsageAliasesTable Data="@_recentUsageStats?.TopUsersByAliases72h" />

        <!-- Top Users by Emails Received ---->
        <RecentUsageEmailsTable Data="@_recentUsageStats?.TopUsersByEmails72h" />

        <!-- Top IP Addresses by Registrations ---->
        <RecentUsageRegistrationsTable Data="@_recentUsageStats?.TopIpsByRegistrations72h" />

        <!-- Top IP Addresses by Mobile Login Requests ---->
        <RecentUsageMobileLoginsTable Data="@_recentUsageStats?.TopIpsByMobileLogins72h" />

        <!-- Top IP Addresses by Account Deletions ---->
        <RecentUsageDeletionsByIpTable Data="@_recentUsageStats?.TopIpsByDeletions30d" />

        <!-- Top Usernames by Account Deletions ---->
        <RecentUsageAccountDeletionsTable Data="@_recentUsageStats?.TopUsernamesByDeletions30d" />
    </div>

    @if (_loadingError)
    {
        <div class="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-4">
            <div class="flex">
                <div class="flex-shrink-0">
                    <svg class="h-5 w-5 text-red-400" fill="currentColor" viewBox="0 0 20 20">
                        <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd"/>
                    </svg>
                </div>
                <div class="ml-3">
                    <h3 class="text-sm font-medium text-red-800 dark:text-red-200">Error Loading Statistics</h3>
                    <p class="text-sm text-red-700 dark:text-red-300 mt-1">Unable to retrieve recent usage statistics. Please try refreshing the page.</p>
                </div>
            </div>
        </div>
    }
</div>

@code {
    private RecentUsageStatistics? _recentUsageStats;
    private bool _loadingError = false;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = "Recent Usage Statistics" });

        // Load statistics asynchronously
        _ = LoadRecentUsageStatisticsAsync();
    }

    private void RefreshData()
    {
        // Reset data
        _recentUsageStats = null;
        _loadingError = false;

        StateHasChanged();

        // Reload statistics
        _ = LoadRecentUsageStatisticsAsync();
    }

    private async Task LoadRecentUsageStatisticsAsync()
    {
        try
        {
            _recentUsageStats = await StatisticsService.GetRecentUsageStatisticsAsync();
            StateHasChanged();
        }
        catch (Exception ex)
        {
            GlobalNotificationService.AddErrorMessage($"Error loading recent usage statistics: {ex.Message}");
            _loadingError = true;
            StateHasChanged();
        }
    }
}
