using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Plugin.SRFPlay.ScheduledTasks;

/// <summary>
/// Scheduled task that clears stale SRF Play resume points from "Continue Watching".
/// </summary>
public class ResumeCleanupTask : IScheduledTask
{
    private readonly ILogger<ResumeCleanupTask> _logger;
    private readonly IResumeCleanupService _resumeCleanupService;

    /// <summary>
    /// Initializes a new instance of the <see cref="ResumeCleanupTask"/> class.
    /// </summary>
    /// <param name="logger">The logger.</param>
    /// <param name="resumeCleanupService">The resume cleanup service.</param>
    public ResumeCleanupTask(
        ILogger<ResumeCleanupTask> logger,
        IResumeCleanupService resumeCleanupService)
    {
        _logger = logger;
        _resumeCleanupService = resumeCleanupService;
    }

    /// <inheritdoc />
    public string Name => "Clean Up SRF Play Continue Watching";

    /// <inheritdoc />
    public string Description => "Removes stale SRF Play resume points - ended livestreams, abandoned playback and finished programmes - from every user's Continue Watching row";

    /// <inheritdoc />
    public string Category => "SRF Play";

    /// <inheritdoc />
    public string Key => "SRFPlayResumeCleanup";

    /// <inheritdoc />
    public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Starting SRF Play Continue Watching cleanup");
        progress?.Report(0);

        try
        {
            var result = _resumeCleanupService.Cleanup(false, cancellationToken);

            progress?.Report(100);
            _logger.LogInformation(
                "SRF Play Continue Watching cleanup completed. Cleared {Cleared} of {Inspected} resume points",
                result.Cleared,
                result.Inspected);
        }
        catch (OperationCanceledException)
        {
            _logger.LogInformation("SRF Play Continue Watching cleanup was cancelled");
            throw;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error during SRF Play Continue Watching cleanup");
            throw;
        }

        return Task.CompletedTask;
    }

    /// <inheritdoc />
    public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
    {
        // Runs after the 3 AM expiration check so items deleted there are already gone.
        return new[]
        {
            new TaskTriggerInfo
            {
                Type = TaskTriggerInfo.TriggerDaily,
                TimeOfDayTicks = TimeSpan.FromHours(4).Ticks
            }
        };
    }
}
