using LiteCharms.Features.Models; using LiteCharms.Features.Shop.Notifications; using LiteCharms.Features.Shop.Notifications.Models; namespace ShopAdmin.Components.Pages; public partial class Notifications(NotificationService notificationService, IToastService ToastService) { protected IQueryable NotificationQueryable { get { var query = NotificationItems.AsQueryable(); if (!string.IsNullOrWhiteSpace(SearchFilter)) { query = query.Where(n => (n.Subject != null && n.Subject.Contains(SearchFilter, StringComparison.OrdinalIgnoreCase)) || (n.SenderAddress != null && n.SenderAddress.Contains(SearchFilter, StringComparison.OrdinalIgnoreCase)) || (n.RecipientAddress != null && n.RecipientAddress.Contains(SearchFilter, StringComparison.OrdinalIgnoreCase))); } return query; } } protected List NotificationItems { get; set; } = []; protected PaginationState Pagination { get; set; } = new() { ItemsPerPage = 10 }; protected bool IsLoading { get; set; } = true; protected string SearchFilter { get; set; } = string.Empty; protected int MaxRecords { get; set; } = 100; protected DateTime FromDate { get; set; } = DateTime.Today.AddDays(-30); protected DateTime ToDate { get; set; } = DateTime.Today.AddDays(1); protected string FromDateBind { get => FromDate.ToString("yyyy-MM-dd"); set { if (DateTime.TryParse(value, out var parsedDate)) { FromDate = parsedDate; } } } protected string ToDateBind { get => ToDate.ToString("yyyy-MM-dd"); set { if (DateTime.TryParse(value, out var parsedDate)) { ToDate = parsedDate; } } } protected int TotalCount => NotificationItems.Count; protected int ErrorCount => NotificationItems.Count(n => n.HasError); protected int PendingCount => NotificationItems.Count(n => !n.Processed && !n.HasError); protected double SuccessRate => TotalCount > 0 ? Math.Round(((double)(TotalCount - ErrorCount) / TotalCount) * 100, 1) : 100.0; protected GridSort SortByOrigin => GridSort.ByAscending(n => n.Platform); protected GridSort SortByPriority => GridSort.ByDescending(n => n.Priority); protected GridSort SortByCreatedAt => GridSort.ByDescending(n => n.CreatedAt); protected GridSort SortBySender => GridSort.ByAscending(n => n.SenderAddress); protected GridSort SortBySubject => GridSort.ByAscending(n => n.Subject); protected GridSort SortByRecipient => GridSort.ByAscending(n => n.RecipientAddress); protected GridSort SortByStatus => GridSort.ByAscending(n => n.HasError); protected override async Task OnInitializedAsync() => await LoadNotificationDataAsync(); protected async Task HandleRefreshClickAsync() { await LoadNotificationDataAsync(); ToastService.ShowInfo("Console metrics and telemetry logs refreshed."); } private async Task LoadNotificationDataAsync() { IsLoading = true; var dateRange = new DateRange() { From = DateOnly.FromDateTime(FromDate), To = DateOnly.FromDateTime(ToDate), MaxRecords = MaxRecords }; var result = await notificationService.GetNotificationsAsync(dateRange); if (result.IsSuccess) NotificationItems = [.. result.Value]; if (result.IsFailed) { var errorMsg = result.Errors.FirstOrDefault()?.Message ?? "Unable to load telemetry log data."; ToastService.ShowError(errorMsg); NotificationItems = []; } IsLoading = false; } protected string GetRowClass(Notification item) { var errorStatus = item.HasError ? "row-has-error" : "row-success"; var priorityStatus = $"row-priority-{item.Priority.ToString().ToLower()}"; return $"{errorStatus} {priorityStatus}"; } protected async Task HandleRetryRowAsync(Notification item) { var updateRequest = new UpdateNotification { NotificationId = item.Id, Processed = false, HasError = false, Errors = null }; var result = await notificationService.UpdateNotificationAsync(updateRequest); await LoadNotificationDataAsync(); if (result.IsSuccess) ToastService.ShowSuccess($"{item.Subject}, has been marked for reprocessing."); if (result.IsFailed) ToastService.ShowError("Failed to update notification state execution."); } }