@using AliasVault.Admin.Main.Models
@using AliasVault.RazorComponents.Tables

<div class="p-4 bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 sm:p-6 dark:bg-gray-800">
    <div class="flex items-center justify-between mb-4">
        <div>
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white">Top IP Addresses by Account Deletions (Last 30d)</h3>
            <p class="text-sm text-gray-500 dark:text-gray-400">IP addresses with the most account deletions in the last 30 days (last octet anonymized)</p>
        </div>
    </div>

    @if (Data != null && Data.Any())
    {
        <div class="mb-3">
            <Paginator CurrentPage="@CurrentPage" PageSize="@PageSize" TotalRecords="@Data.Count" OnPageChanged="@HandlePageChanged" />
        </div>
        <div class="overflow-x-auto">
            <SortableTable Columns="@_tableColumns">
                @foreach (var ip in PagedData)
                {
                    <SortableTableRow>
                        <SortableTableColumn IsPrimary="true">
                            <a href="logging/auth?search=@Uri.EscapeDataString(ip.OriginalIpAddress)" class="font-mono text-gray-900 dark:text-gray-100 hover:text-blue-600 dark:hover:text-blue-400 cursor-pointer">
                                @ip.IpAddress
                            </a>
                        </SortableTableColumn>
                        <SortableTableColumn>@ip.DeletionCount30d.ToString("N0")</SortableTableColumn>
                    </SortableTableRow>
                }
            </SortableTable>
        </div>
    }
    else if (Data != null)
    {
        <div class="text-center text-gray-500 dark:text-gray-400">
            <svg class="mx-auto h-12 w-12 text-gray-300 dark:text-gray-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
            </svg>
            <h3 class="mt-2 text-sm font-medium text-gray-900 dark:text-white">No Recent Account Deletions</h3>
            <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">No account deletions occurred in the last 30 days.</p>
        </div>
    }
    else
    {
        <div class="px-6 py-8 flex justify-center">
            <LoadingIndicator />
        </div>
    }
</div>

@code {
    [Parameter]
    public List<RecentUsageDeletionsByIp>? Data { get; set; }

    private int CurrentPage { get; set; } = 1;
    private int PageSize { get; set; } = 10;

    private IEnumerable<RecentUsageDeletionsByIp> PagedData =>
        Data?.Skip((CurrentPage - 1) * PageSize).Take(PageSize) ?? Enumerable.Empty<RecentUsageDeletionsByIp>();

    private readonly List<TableColumn> _tableColumns = new()
    {
        new() { Title = "IP Range", PropertyName = "IpAddress", Sortable = false },
        new() { Title = "Deletions (30d)", PropertyName = "DeletionCount30d", Sortable = false }
    };

    private void HandlePageChanged(int page)
    {
        CurrentPage = page;
        StateHasChanged();
    }
}
