#nullable enable
using System;
using System.Threading;
using System.Threading.Tasks;
using Baya.Application.Contracts.Notifications;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Baya.Infrastructure.Persistence.Services.Scheduling.Jobs;

/// <summary>
/// Hard-deletes read notifications older than the retention window (re-homed from the b1
/// <c>NotificationRetentionHostedService</c>). Unread notifications are never deleted. Retention window and
/// cadence are documented constants (no config key).
/// </summary>
internal sealed class NotificationRetentionJob(ILogger<NotificationRetentionJob> logger) : IRecurringJob
{
    private const int RetentionDays = 90;

    public string Name => "notification_retention";

    public ValueTask<TimeSpan> GetIntervalAsync(IServiceProvider services, CancellationToken cancellationToken)
        => ValueTask.FromResult(TimeSpan.FromHours(24));

    public async ValueTask RunAsync(IServiceProvider services, CancellationToken cancellationToken)
    {
        var notifications = services.GetRequiredService<INotificationService>();
        var removed = await notifications.PurgeOldReadAsync(RetentionDays, cancellationToken);

        if (removed > 0)
            logger.LogInformation("Notification retention purged {Count} read notifications older than {Days}d", removed, RetentionDays);
    }
}
