@page "/notifications/report"
@attribute [Authorize(Policy = ScadaLink.Security.AuthorizationPolicies.RequireDeployment)]
@using ScadaLink.Commons.Entities.Sites
@using ScadaLink.Commons.Interfaces.Repositories
@using ScadaLink.Commons.Messages.Notification
@using ScadaLink.Communication
@inject CommunicationService CommunicationService
@inject ISiteRepository SiteRepository
@inject IDialogService Dialog
@inject ILogger<NotificationReport> Logger

<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 Report</h4>
        <button class="btn btn-outline-secondary btn-sm" @onclick="RefreshAll" disabled="@_loading">
            @if (_loading) { <span class="spinner-border spinner-border-sm me-1" role="status"></span> }
            Refresh
        </button>
    </div>

    @* ── Filters ── *@
    <div class="card mb-3">
        <div class="card-body py-2">
            <div class="row g-2 align-items-end">
                <div class="col-auto">
                    <label class="form-label small mb-1" for="no-status">Status</label>
                    <select id="no-status" class="form-select form-select-sm" style="min-width: 130px;"
                            @bind="_statusFilter">
                        <option value="">All</option>
                        <option value="Pending">Pending</option>
                        <option value="Retrying">Retrying</option>
                        <option value="Delivered">Delivered</option>
                        <option value="Parked">Parked</option>
                        <option value="Discarded">Discarded</option>
                    </select>
                </div>
                <div class="col-auto">
                    <label class="form-label small mb-1" for="no-type">Type</label>
                    <select id="no-type" class="form-select form-select-sm" style="min-width: 120px;"
                            @bind="_typeFilter">
                        <option value="">All</option>
                        <option value="Email">Email</option>
                    </select>
                </div>
                <div class="col-auto">
                    <label class="form-label small mb-1" for="no-site">Source site</label>
                    <select id="no-site" class="form-select form-select-sm" style="min-width: 150px;"
                            @bind="_siteFilter">
                        <option value="">Any</option>
                        @foreach (var site in _sites)
                        {
                            <option value="@site.SiteIdentifier">@site.Name</option>
                        }
                    </select>
                </div>
                <div class="col-auto">
                    <label class="form-label small mb-1" for="no-list">List name</label>
                    <input id="no-list" type="text" class="form-control form-control-sm"
                           style="min-width: 140px;" placeholder="Any" @bind="_listFilter" />
                </div>
                <div class="col-auto">
                    <label class="form-label small mb-1" for="no-from">From</label>
                    <input id="no-from" type="datetime-local" class="form-control form-control-sm"
                           @bind="_fromFilter" />
                </div>
                <div class="col-auto">
                    <label class="form-label small mb-1" for="no-to">To</label>
                    <input id="no-to" type="datetime-local" class="form-control form-control-sm"
                           @bind="_toFilter" />
                </div>
                <div class="col">
                    <label class="form-label small mb-1" for="no-search">Subject keyword</label>
                    <input id="no-search" type="search" class="form-control form-control-sm"
                           placeholder="Search subject…" @bind="_subjectFilter" />
                </div>
                <div class="col-auto">
                    <div class="form-check mb-1">
                        <input class="form-check-input" type="checkbox" id="no-stuck-only"
                               @bind="_stuckOnly" />
                        <label class="form-check-label small" for="no-stuck-only">Stuck only</label>
                    </div>
                </div>
                <div class="col-auto">
                    <button class="btn btn-outline-secondary btn-sm" @onclick="ClearFilters"
                            disabled="@(!HasActiveFilters)">Clear</button>
                </div>
                <div class="col-auto">
                    <button class="btn btn-primary btn-sm" @onclick="Search" disabled="@_loading">
                        @if (_loading) { <span class="spinner-border spinner-border-sm me-1" role="status"></span> }
                        Query
                    </button>
                </div>
            </div>
        </div>
    </div>

    @if (_listError != null)
    {
        <div class="alert alert-danger">@_listError</div>
    }

    @* ── Notification list ── *@
    @if (_notifications == null)
    {
        @if (_loading)
        {
            <div class="text-muted small">Loading…</div>
        }
    }
    else if (_notifications.Count == 0)
    {
        <div class="card">
            <div class="card-body text-center text-muted py-5">
                <div class="fs-5 mb-1">No notifications</div>
                <div class="small">No notifications match the current filters.</div>
            </div>
        </div>
    }
    else
    {
        <div class="table-responsive">
            <table class="table table-sm table-hover align-middle">
                <thead class="table-light">
                    <tr>
                        <th>ID</th>
                        <th>Type</th>
                        <th>List</th>
                        <th>Subject</th>
                        <th>Status</th>
                        <th class="text-end">Retries</th>
                        <th>Source site</th>
                        <th>Created</th>
                        <th>Delivered</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var n in _notifications)
                    {
                        <tr @key="n.NotificationId" class="@(n.IsStuck ? "table-warning" : "")">
                            <td><code class="small" title="@n.NotificationId">@ShortId(n.NotificationId)</code></td>
                            <td>@n.Type</td>
                            <td>@n.ListName</td>
                            <td>
                                @n.Subject
                                @if (!string.IsNullOrEmpty(n.LastError))
                                {
                                    <div class="small text-danger text-truncate" style="max-width: 320px;"
                                         title="@n.LastError">@n.LastError</div>
                                }
                            </td>
                            <td>
                                <span class="badge @StatusBadgeClass(n.Status)">@n.Status</span>
                                @if (n.IsStuck)
                                {
                                    <span class="badge bg-warning text-dark ms-1">Stuck</span>
                                }
                            </td>
                            <td class="text-end font-monospace">@n.RetryCount</td>
                            <td><span class="small">@SiteName(n.SourceSiteId)</span></td>
                            <td><TimestampDisplay Value="@n.CreatedAt" Format="yyyy-MM-dd HH:mm" /></td>
                            <td><TimestampDisplay Value="@n.DeliveredAt" Format="yyyy-MM-dd HH:mm" NullText="—" /></td>
                            <td class="text-end">
                                @if (n.Status == "Parked")
                                {
                                    <button class="btn btn-outline-success btn-sm me-1"
                                            @onclick="() => RetryNotification(n)" disabled="@_actionInProgress">
                                        Retry
                                    </button>
                                    <button class="btn btn-outline-danger btn-sm"
                                            @onclick="() => DiscardNotification(n)" disabled="@_actionInProgress">
                                        Discard
                                    </button>
                                }
                                else
                                {
                                    <span class="text-muted small">—</span>
                                }
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>

        @if (_totalCount > _pageSize)
        {
            <div class="d-flex justify-content-between align-items-center">
                <span class="text-muted small">
                    Page @_pageNumber of @((_totalCount + _pageSize - 1) / _pageSize) · @_totalCount total
                </span>
                <div>
                    <button class="btn btn-outline-secondary btn-sm me-1"
                            @onclick="PrevPage" disabled="@(_pageNumber <= 1 || _loading)">Previous</button>
                    <button class="btn btn-outline-secondary btn-sm"
                            @onclick="NextPage" disabled="@(_notifications.Count < _pageSize || _loading)">Next</button>
                </div>
            </div>
        }
    }
</div>

@code {
    private const int _pageSize = 50;

    private ToastNotification _toast = default!;
    private List<Site> _sites = new();

    // List
    private List<NotificationSummary>? _notifications;
    private int _totalCount;
    private int _pageNumber = 1;
    private bool _loading;
    private string? _listError;
    private bool _actionInProgress;

    // Filters
    private string _statusFilter = string.Empty;
    private string _typeFilter = string.Empty;
    private string _siteFilter = string.Empty;
    private string _listFilter = string.Empty;
    private string _subjectFilter = string.Empty;
    private bool _stuckOnly;
    private DateTime? _fromFilter;
    private DateTime? _toFilter;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            _sites = (await SiteRepository.GetAllSitesAsync()).ToList();
        }
        catch (Exception ex)
        {
            // Non-fatal — source-site filter just falls back to the raw site IDs.
            Logger.LogWarning(ex, "Failed to load sites for the report source-site filter.");
        }

        await RefreshAll();
    }

    private async Task RefreshAll()
    {
        await FetchPage();
    }

    private async Task Search()
    {
        _pageNumber = 1;
        await FetchPage();
    }

    private async Task PrevPage() { _pageNumber--; await FetchPage(); }
    private async Task NextPage() { _pageNumber++; await FetchPage(); }

    private async Task FetchPage()
    {
        _loading = true;
        _listError = null;
        try
        {
            var request = new NotificationOutboxQueryRequest(
                CorrelationId: Guid.NewGuid().ToString("N"),
                StatusFilter: NullIfEmpty(_statusFilter),
                TypeFilter: NullIfEmpty(_typeFilter),
                SourceSiteFilter: NullIfEmpty(_siteFilter),
                ListNameFilter: NullIfEmpty(_listFilter),
                StuckOnly: _stuckOnly,
                SubjectKeyword: NullIfEmpty(_subjectFilter),
                From: ToUtc(_fromFilter),
                To: ToUtc(_toFilter),
                PageNumber: _pageNumber,
                PageSize: _pageSize);

            var response = await CommunicationService.QueryNotificationOutboxAsync(request);
            if (response.Success)
            {
                _notifications = response.Notifications.ToList();
                _totalCount = response.TotalCount;
            }
            else
            {
                _listError = response.ErrorMessage ?? "Query failed.";
            }
        }
        catch (Exception ex)
        {
            _listError = $"Query failed: {ex.Message}";
        }
        _loading = false;
    }

    private async Task RetryNotification(NotificationSummary n)
    {
        var confirmed = await Dialog.ConfirmAsync(
            "Retry notification",
            $"Re-queue notification {ShortId(n.NotificationId)} (\"{n.Subject}\") for delivery?");
        if (!confirmed) return;

        _actionInProgress = true;
        try
        {
            var response = await CommunicationService.RetryNotificationAsync(
                new RetryNotificationRequest(Guid.NewGuid().ToString("N"), n.NotificationId));
            if (response.Success)
            {
                _toast.ShowSuccess($"Notification {ShortId(n.NotificationId)} re-queued for delivery.");
                await RefreshAll();
            }
            else
            {
                _toast.ShowError(response.ErrorMessage ?? "Retry failed.");
            }
        }
        catch (Exception ex)
        {
            _toast.ShowError($"Retry failed: {ex.Message}");
        }
        _actionInProgress = false;
    }

    private async Task DiscardNotification(NotificationSummary n)
    {
        var confirmed = await Dialog.ConfirmAsync(
            "Discard notification",
            $"Permanently discard notification {ShortId(n.NotificationId)} (\"{n.Subject}\")? This cannot be undone.",
            danger: true);
        if (!confirmed) return;

        _actionInProgress = true;
        try
        {
            var response = await CommunicationService.DiscardNotificationAsync(
                new DiscardNotificationRequest(Guid.NewGuid().ToString("N"), n.NotificationId));
            if (response.Success)
            {
                _toast.ShowSuccess($"Notification {ShortId(n.NotificationId)} discarded.");
                await RefreshAll();
            }
            else
            {
                _toast.ShowError(response.ErrorMessage ?? "Discard failed.");
            }
        }
        catch (Exception ex)
        {
            _toast.ShowError($"Discard failed: {ex.Message}");
        }
        _actionInProgress = false;
    }

    private void ClearFilters()
    {
        _statusFilter = string.Empty;
        _typeFilter = string.Empty;
        _siteFilter = string.Empty;
        _listFilter = string.Empty;
        _subjectFilter = string.Empty;
        _stuckOnly = false;
        _fromFilter = null;
        _toFilter = null;
    }

    private bool HasActiveFilters =>
        !string.IsNullOrEmpty(_statusFilter) ||
        !string.IsNullOrEmpty(_typeFilter) ||
        !string.IsNullOrEmpty(_siteFilter) ||
        !string.IsNullOrEmpty(_listFilter) ||
        !string.IsNullOrEmpty(_subjectFilter) ||
        _stuckOnly ||
        _fromFilter != null ||
        _toFilter != null;

    private string SiteName(string siteId) =>
        _sites.FirstOrDefault(s => s.SiteIdentifier == siteId)?.Name ?? siteId;

    private static string? NullIfEmpty(string s) => string.IsNullOrWhiteSpace(s) ? null : s.Trim();

    private static DateTimeOffset? ToUtc(DateTime? local) =>
        local == null ? null : new DateTimeOffset(DateTime.SpecifyKind(local.Value, DateTimeKind.Utc));

    private static string ShortId(string id) => id[..Math.Min(12, id.Length)];

    private static string StatusBadgeClass(string status) => status switch
    {
        "Delivered" => "bg-success",
        "Parked" => "bg-danger",
        "Retrying" => "bg-warning text-dark",
        "Pending" => "bg-info text-dark",
        "Discarded" => "bg-secondary",
        _ => "bg-light text-dark"
    };
}
