using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Search;
using ErsatzTV.Core.Search;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Infrastructure.Extensions;
using Microsoft.EntityFrameworkCore;

namespace ErsatzTV.Application.MediaCollections;

public class DeleteSmartCollectionHandler : IRequestHandler<DeleteSmartCollection, Either<BaseError, Unit>>
{
    private readonly IDbContextFactory<TvContext> _dbContextFactory;
    private readonly ISearchTargets _searchTargets;
    private readonly ISmartCollectionCache _smartCollectionCache;

    public DeleteSmartCollectionHandler(
        IDbContextFactory<TvContext> dbContextFactory,
        ISearchTargets searchTargets,
        ISmartCollectionCache smartCollectionCache)
    {
        _dbContextFactory = dbContextFactory;
        _searchTargets = searchTargets;
        _smartCollectionCache = smartCollectionCache;
    }

    public async Task<Either<BaseError, Unit>> Handle(
        DeleteSmartCollection request,
        CancellationToken cancellationToken)
    {
        await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
        Option<SmartCollection> maybeSmartCollection = await SmartCollectionMustExist(
            dbContext,
            request,
            cancellationToken);
        return await maybeSmartCollection.Match(
            Some: smartCollection => DoDeletion(dbContext, smartCollection, cancellationToken).Map(Right<BaseError, Unit>),
            None: () => Task.FromResult<Either<BaseError, Unit>>(
                new NotFoundError($"SmartCollection {request.SmartCollectionId} does not exist.")));
    }

    private async Task<Unit> DoDeletion(
        TvContext dbContext,
        SmartCollection smartCollection,
        CancellationToken cancellationToken)
    {
        dbContext.SmartCollections.Remove(smartCollection);
        await dbContext.SaveChangesAsync(cancellationToken);
        _searchTargets.SearchTargetsChanged();

        // post-commit side effect runs on CancellationToken.None so a late request cancellation
        // can't abort it after the commit landed (#254)
        await _smartCollectionCache.Refresh(CancellationToken.None);
        return Unit.Default;
    }

    private static Task<Option<SmartCollection>> SmartCollectionMustExist(
        TvContext dbContext,
        DeleteSmartCollection request,
        CancellationToken cancellationToken) =>
        dbContext.SmartCollections
            .SelectOneAsync(c => c.Id, c => c.Id == request.SmartCollectionId, cancellationToken)
            .Map(identity);
}
