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

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

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

public class DeleteEpisodeCommandHandler(IAnimeTitleGateway titleGateway) : IRequestHandler<DeleteEpisodeCommand, Unit>
{
    public async Task<Unit> Handle(DeleteEpisodeCommand request, CancellationToken cancellationToken)
    {
        var title = await titleGateway.GetById(request.TitleId);
        var season = title.Items.OfType<AnimeSeason>().First(q => q.Id == request.SeasonId);
        var episode = season.Episodes.First(q => q.Id == request.EpisodeId);
        episode.Delete();
        await titleGateway.Update(title);
        return Unit.Value;
    }
}