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

namespace TakeoutSaaS.Module.Scheduler.Jobs;

/// <summary>
/// 账单逾期标记任务：将超过到期日的待支付账单标记为逾期。
/// </summary>
public sealed class BillingOverdueProcessJob(
    IMediator mediator,
    IOptionsMonitor<BillingAutomationOptions> optionsMonitor,
    ILogger<BillingOverdueProcessJob> logger)
{
    /// <summary>
    /// 执行逾期账单标记。
    /// </summary>
    public async Task ExecuteAsync()
    {
        // 1. 读取配置并执行逾期处理
        var options = optionsMonitor.CurrentValue;
        var updatedCount = await mediator.Send(new ProcessOverdueBillingsCommand());

        // 2. 记录执行结果
        logger.LogInformation(
            "定时任务：逾期账单标记完成，更新 {UpdatedCount} 条（Cron={Cron}）",
            updatedCount,
            options.OverdueBillingProcessCron);
    }
}
