@page "/notifications/lists"
@using ScadaLink.Security
@using ScadaLink.Commons.Entities.Notifications
@using ScadaLink.Commons.Interfaces.Repositories
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDesign)]
@inject INotificationRepository NotificationRepository
@inject NavigationManager NavigationManager
@inject IDialogService Dialog

<div class="container-fluid mt-3">
    <ToastNotification @ref="_toast" />

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="mb-0">Notification Lists</h4>
        <button class="btn btn-primary btn-sm"
                @onclick='() => NavigationManager.NavigateTo("/notifications/lists/create")'>
            Add Notification List
        </button>
    </div>

    @if (_loading)
    {
        <LoadingSpinner IsLoading="true" />
    }
    else if (_errorMessage != null)
    {
        <div class="alert alert-danger">@_errorMessage</div>
    }
    else if (_lists.Count == 0)
    {
        <div class="card">
            <div class="card-body text-center text-muted py-5">
                <div class="fs-5 mb-2">No notification lists</div>
                <button class="btn btn-primary btn-sm"
                        @onclick='() => NavigationManager.NavigateTo("/notifications/lists/create")'>
                    Add your first notification list
                </button>
            </div>
        </div>
    }
    else
    {
        <div class="table-responsive">
            <table class="table table-sm table-hover align-middle">
                <thead class="table-light">
                    <tr>
                        <th>Name</th>
                        <th>Recipients</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var list in _lists)
                    {
                        var recipients = _recipients.GetValueOrDefault(list.Id)
                            ?? (IReadOnlyList<NotificationRecipient>)Array.Empty<NotificationRecipient>();
                        <tr @key="list.Id">
                            <td>@list.Name</td>
                            <td>
                                @if (recipients.Count == 0)
                                {
                                    <span class="text-muted small fst-italic">No recipients</span>
                                }
                                else
                                {
                                    @foreach (var r in recipients)
                                    {
                                        <span class="badge bg-light text-dark me-1 mb-1">@r.Name &lt;@r.EmailAddress&gt;</span>
                                    }
                                }
                            </td>
                            <td class="text-end">
                                <button class="btn btn-outline-primary btn-sm me-1"
                                        @onclick='() => NavigationManager.NavigateTo($"/notifications/lists/{list.Id}/edit")'>
                                    Edit
                                </button>
                                <button class="btn btn-outline-danger btn-sm"
                                        @onclick="() => DeleteList(list)">
                                    Delete
                                </button>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    }
</div>

@code {
    private bool _loading = true;
    private string? _errorMessage;
    private List<NotificationList> _lists = new();
    private readonly Dictionary<int, IReadOnlyList<NotificationRecipient>> _recipients = new();
    private ToastNotification _toast = default!;

    protected override async Task OnInitializedAsync() => await LoadAsync();

    private async Task LoadAsync()
    {
        _loading = true;
        _errorMessage = null;
        try
        {
            _lists = (await NotificationRepository.GetAllNotificationListsAsync()).ToList();
            _recipients.Clear();
            foreach (var list in _lists)
            {
                _recipients[list.Id] = await NotificationRepository.GetRecipientsByListIdAsync(list.Id);
            }
        }
        catch (Exception ex)
        {
            _errorMessage = $"Failed to load notification lists: {ex.Message}";
        }
        _loading = false;
    }

    private async Task DeleteList(NotificationList list)
    {
        if (!await Dialog.ConfirmAsync("Delete", $"Delete notification list '{list.Name}'?", danger: true))
        {
            return;
        }
        try
        {
            await NotificationRepository.DeleteNotificationListAsync(list.Id);
            await NotificationRepository.SaveChangesAsync();
            _toast.ShowSuccess("Deleted.");
            await LoadAsync();
        }
        catch (Exception ex)
        {
            _toast.ShowError($"Failed to delete notification list: {ex.Message}");
        }
    }
}
