using MediatR;
using Modules.Library.Application.Gateways;
using Modules.Library.Domain.Entities.MediaContent.Items.Anime;

namespace Modules.Library.Application.Commands.Anime.Title;

public class DeleteEpisodeCommand : IRequest<Unit>
{
    public Guid TitleId { get; set; }
    public Guid EpisodeId { get; set; }
    public bool DisableSoftDelete { get; set; }
}

public class DeleteEpisodeCommandHandler(IAnimeTitleGateway titleGateway, IAnimeEpisodeGateway episodeGateway) : IRequestHandler<DeleteEpisodeCommand, Unit>
{
    public async Task<Unit> Handle(DeleteEpisodeCommand request, CancellationToken cancellationToken)
    {
        var title = await titleGateway.GetById(request.TitleId);
        var episode = title.Items.OfType<AnimeEpisode>().First(q => q.Id == request.EpisodeId);
        episode.Delete();
        await titleGateway.Update(title);
        if (request.DisableSoftDelete)
        {
            await episodeGateway.Delete(title.Id, null, episode);
        }
        else
        {
            await episodeGateway.Update(title.Id, null, episode);
        }
        return Unit.Value;
    }
}