using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.Moderation.Actions;
using BirdsiteLive.Pipeline.Contracts;
using BirdsiteLive.Pipeline.Models;

using Microsoft.Extensions.Logging;

namespace BirdsiteLive.Pipeline.Processors;

public class SaveProgressionProcessor : ISaveProgressionProcessor
{
    private readonly ITwitterUserDal _twitterUserDal;
    private readonly ILogger<SaveProgressionProcessor> _logger;
    private readonly IRemoveTwitterAccountAction _removeTwitterAccountAction;

    #region Ctor
    public SaveProgressionProcessor(
        ITwitterUserDal twitterUserDal,
        ILogger<SaveProgressionProcessor> logger,
        IRemoveTwitterAccountAction removeTwitterAccountAction
    )
    {
        _twitterUserDal = twitterUserDal;
        _logger = logger;
        _removeTwitterAccountAction = removeTwitterAccountAction;
    }
    #endregion

    public async Task ProcessAsync(UserWithDataToSync userWithTweetsToSync, CancellationToken ct)
    {
        try
        {
            if (userWithTweetsToSync.Tweets.Length == 0)
            {
                _logger.LogWarning("No tweets synchronized");
                return;
            }
            if (userWithTweetsToSync.Followers.Length == 0)
            {
                _logger.LogWarning(
                    "No Followers found for {User}, purging them",
                    userWithTweetsToSync.User.Acct
                );
                await _removeTwitterAccountAction.ProcessAsync(userWithTweetsToSync.User);
                _logger.LogInformation("Account {User} purged", userWithTweetsToSync.User.Acct);

                return;
            }

            var userId = userWithTweetsToSync.User.Id;
            var followingSyncStatuses = userWithTweetsToSync.Followers
                .Select(x => x.FollowingsSyncStatus[userId])
                .ToList();

            if (followingSyncStatuses.Count == 0)
            {
                _logger.LogWarning(
                    "No Followers sync found for {User}, Id: {UserId}",
                    userWithTweetsToSync.User.Acct,
                    userId
                );
                return;
            }

            var lastPostedTweet = userWithTweetsToSync.Tweets.Select(x => x.Id).Max();
            var minimumSync = followingSyncStatuses.Min();
            var now = DateTime.UtcNow;
            await _twitterUserDal.UpdateTwitterUserAsync(
                userId,
                lastPostedTweet,
                minimumSync,
                userWithTweetsToSync.User.FetchingErrorCount,
                now
            );
        }
        catch (Exception e)
        {
            _logger.LogError(e, "SaveProgressionProcessor.ProcessAsync() Exception");
            throw;
        }
    }
}
