using MediatR;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using TakeoutSaaS.Application.App.Subscriptions.Commands;
using TakeoutSaaS.Module.Scheduler.Options;

namespace TakeoutSaaS.Module.Scheduler.Jobs;

/// <summary>
/// 订阅续费提醒任务：到期前 7/3/1 天发送站内提醒。
/// </summary>
public sealed class SubscriptionRenewalReminderJob(
    IMediator mediator,
    IOptionsMonitor<SubscriptionAutomationOptions> optionsMonitor,
    ILogger<SubscriptionRenewalReminderJob> logger)
{
    /// <summary>
    /// 执行续费提醒扫描与发送。
    /// </summary>
    public async Task ExecuteAsync()
    {
        // 1. 读取配置并执行续费提醒
        var options = optionsMonitor.CurrentValue;
        var result = await mediator.Send(new ProcessRenewalRemindersCommand
        {
            ReminderDaysBeforeExpiry = options.ReminderDaysBeforeExpiry
        });

        // 2. 记录执行结果
        logger.LogInformation(
            "定时任务：续费提醒处理完成，候选 {CandidateCount}，创建 {CreatedReminderCount}",
            result.CandidateCount,
            result.CreatedReminderCount);
    }
}
