using LiteCqrs;
using Microsoft.EntityFrameworkCore;
using TeleWave.Application.Common.Interfaces;
using TeleWave.Application.Common.Models;

namespace TeleWave.Application.Library.Collections.RemoveCollectionShow;

public sealed class RemoveCollectionShowCommandHandler(IAppDbContext dbContext)
    : ICommandHandler<RemoveCollectionShowCommand, Result>
{
    public async Task<Result> Handle(
        RemoveCollectionShowCommand command,
        CancellationToken cancellationToken
    )
    {
        var collection = await dbContext
            .Collections.Include(c => c.Items)
            .FirstOrDefaultAsync(c => c.Id == command.CollectionId, cancellationToken);
        if (collection is null)
            return Result.Failure(CollectionErrors.NotFound);

        return collection.RemoveShow(command.ShowId)
            ? Result.Success()
            : Result.Failure(CollectionErrors.ShowNotInCollection);
    }
}
