using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using StellaOps.Feedser.Core.Jobs;

namespace StellaOps.Feedser.Exporter.Json;

public sealed class JsonExportJob : IJob
{
    public const string JobKind = "export:json";
    public static readonly TimeSpan DefaultTimeout = TimeSpan.FromMinutes(10);
    public static readonly TimeSpan DefaultLeaseDuration = TimeSpan.FromMinutes(5);

    private readonly JsonFeedExporter _exporter;
    private readonly ILogger<JsonExportJob> _logger;

    public JsonExportJob(JsonFeedExporter exporter, ILogger<JsonExportJob> logger)
    {
        _exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public async Task ExecuteAsync(JobExecutionContext context, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Executing JSON export job {RunId}", context.RunId);
        await _exporter.ExportAsync(context.Services, cancellationToken).ConfigureAwait(false);
        _logger.LogInformation("Completed JSON export job {RunId}", context.RunId);
    }
}
