using System.Text.Json; using maintenance.Data; using maintenance.Data.Entities; using Microsoft.EntityFrameworkCore; namespace maintenance.Services.Pages; public sealed class ManagedPageService : IManagedPageService { private readonly AppDbContext _dbContext; private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; public ManagedPageService(AppDbContext dbContext) { _dbContext = dbContext; } public async Task ResolveAsync( string pageKind, string? requestedPageKey = null, DateTime? nowUtc = null, CancellationToken cancellationToken = default) { var normalizedKind = NormalizeKind(pageKind); var utcNow = nowUtc ?? DateTime.UtcNow; var pages = await _dbContext.ManagedPages .AsNoTracking() .Where(p => p.PageKind == normalizedKind && p.IsEnabled) .OrderByDescending(p => p.OverrideWindow) .ThenByDescending(p => p.Priority) .ThenBy(p => p.Key) .ToListAsync(cancellationToken); ManagedPageEntity? selected = null; if (!string.IsNullOrWhiteSpace(requestedPageKey)) { selected = pages.FirstOrDefault(p => p.Key.Equals(requestedPageKey, StringComparison.OrdinalIgnoreCase) && IsWindowActive(p, utcNow)); } selected ??= pages.FirstOrDefault(p => p.OverrideWindow && IsWindowActive(p, utcNow)); selected ??= pages.FirstOrDefault(p => !p.IsDefaultFallback && IsWindowActive(p, utcNow)); selected ??= pages.FirstOrDefault(p => p.IsDefaultFallback); if (selected is null) { return new ResolvedPage { PageKind = normalizedKind, PageKey = normalizedKind == "maintenance" ? "generic-maintenance" : "generic-error", IsDefaultFallback = true, Content = DefaultContent(normalizedKind) }; } return new ResolvedPage { PageKind = normalizedKind, PageKey = selected.Key, IsDefaultFallback = selected.IsDefaultFallback, Content = DeserializeContent(selected.ContentJson, normalizedKind) }; } private static string NormalizeKind(string pageKind) { return pageKind.Equals("error", StringComparison.OrdinalIgnoreCase) ? "error" : "maintenance"; } private static bool IsWindowActive(ManagedPageEntity page, DateTime utcNow) { // Initial behavior: // - If no window set => active. // - If one-time window set => active only inside start/end. // - RecurrenceRule parsing is future enhancement; currently treated as active when no one-time bounds fail. if (page.StartUtc is null && page.EndUtc is null) { return true; } if (page.StartUtc.HasValue && utcNow < page.StartUtc.Value) { return false; } if (page.EndUtc.HasValue && utcNow > page.EndUtc.Value) { return false; } return true; } private static PageContentModel DeserializeContent(string contentJson, string kind) { if (string.IsNullOrWhiteSpace(contentJson)) { return DefaultContent(kind); } try { return JsonSerializer.Deserialize(contentJson, JsonOptions) ?? DefaultContent(kind); } catch { return DefaultContent(kind); } } private static PageContentModel DefaultContent(string kind) { if (kind == "maintenance") { return new PageContentModel { ErrorName = "Application Unavailable", Description = "MAX applications may be down for scheduled maintenance 2AM-8AM Sunday morning. If this appears at another time, contact support.", ErrorCode = "eau", ShowInstructions = false }; } return new PageContentModel { ErrorName = "System Error", Description = "A system error has occurred. Please contact MAX Support.", ErrorCode = "eag", ShowInstructions = false }; } }