@page "/admin/api-keys"
@using ScadaLink.Security
@using ScadaLink.Commons.Entities.InboundApi
@using ScadaLink.Commons.Interfaces.Repositories
@attribute [Authorize(Policy = AuthorizationPolicies.RequireAdmin)]
@inject IInboundApiRepository InboundApiRepository
@inject NavigationManager NavigationManager

<div class="container-fluid mt-3">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="mb-0">API Key Management</h4>
        <button class="btn btn-primary btn-sm" @onclick='() => NavigationManager.NavigateTo("/admin/api-keys/create")'>Add API Key</button>
    </div>

    <ToastNotification @ref="_toast" />
    <ConfirmDialog @ref="_confirmDialog" />

    @if (_loading)
    {
        <LoadingSpinner IsLoading="true" />
    }
    else if (_errorMessage != null)
    {
        <div class="alert alert-danger">@_errorMessage</div>
    }
    else
    {
        <table class="table table-sm table-striped table-hover">
            <thead class="table-dark">
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Key Value</th>
                    <th>Status</th>
                    <th style="width: 240px;">Actions</th>
                </tr>
            </thead>
            <tbody>
                @if (_keys.Count == 0)
                {
                    <tr>
                        <td colspan="5" class="text-muted text-center">No API keys configured.</td>
                    </tr>
                }
                @foreach (var key in _keys)
                {
                    <tr>
                        <td>@key.Id</td>
                        <td>@key.Name</td>
                        <td><code>@MaskKeyValue(key.KeyValue)</code></td>
                        <td>
                            @if (key.IsEnabled)
                            {
                                <span class="badge bg-success">Enabled</span>
                            }
                            else
                            {
                                <span class="badge bg-secondary">Disabled</span>
                            }
                        </td>
                        <td>
                            <button class="btn btn-outline-primary btn-sm py-0 px-1 me-1"
                                    @onclick='() => NavigationManager.NavigateTo($"/admin/api-keys/{key.Id}/edit")'>Edit</button>
                            @if (key.IsEnabled)
                            {
                                <button class="btn btn-outline-warning btn-sm py-0 px-1 me-1"
                                        @onclick="() => ToggleKey(key)">Disable</button>
                            }
                            else
                            {
                                <button class="btn btn-outline-success btn-sm py-0 px-1 me-1"
                                        @onclick="() => ToggleKey(key)">Enable</button>
                            }
                            <button class="btn btn-outline-danger btn-sm py-0 px-1"
                                    @onclick="() => DeleteKey(key)">Delete</button>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    }
</div>

@code {
    private List<ApiKey> _keys = new();
    private bool _loading = true;
    private string? _errorMessage;

    private ToastNotification _toast = default!;
    private ConfirmDialog _confirmDialog = default!;

    protected override async Task OnInitializedAsync()
    {
        await LoadDataAsync();
    }

    private async Task LoadDataAsync()
    {
        _loading = true;
        _errorMessage = null;
        try
        {
            _keys = (await InboundApiRepository.GetAllApiKeysAsync()).ToList();
        }
        catch (Exception ex)
        {
            _errorMessage = $"Failed to load API keys: {ex.Message}";
        }
        _loading = false;
    }

    private static string MaskKeyValue(string keyValue)
    {
        if (keyValue.Length <= 8) return new string('*', keyValue.Length);
        return keyValue[..4] + new string('*', keyValue.Length - 8) + keyValue[^4..];
    }

    private async Task ToggleKey(ApiKey key)
    {
        try
        {
            key.IsEnabled = !key.IsEnabled;
            await InboundApiRepository.UpdateApiKeyAsync(key);
            await InboundApiRepository.SaveChangesAsync();
            _toast.ShowSuccess($"API key '{key.Name}' {(key.IsEnabled ? "enabled" : "disabled")}.");
        }
        catch (Exception ex)
        {
            _toast.ShowError($"Toggle failed: {ex.Message}");
        }
    }

    private async Task DeleteKey(ApiKey key)
    {
        var confirmed = await _confirmDialog.ShowAsync(
            $"Delete API key '{key.Name}'? This cannot be undone.", "Delete API Key");
        if (!confirmed) return;

        try
        {
            await InboundApiRepository.DeleteApiKeyAsync(key.Id);
            await InboundApiRepository.SaveChangesAsync();
            _toast.ShowSuccess($"API key '{key.Name}' deleted.");
            await LoadDataAsync();
        }
        catch (Exception ex)
        {
            _toast.ShowError($"Delete failed: {ex.Message}");
        }
    }

}
