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

namespace Modules.Library.Application.Commands.Anime.Episode.Properties;

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

}

public class SetCompletedCommandHandler(IAnimeTitleGateway titleGateway) : IRequestHandler<SetCompletedCommand, Unit>
{
    public async Task<Unit> Handle(SetCompletedCommand request, CancellationToken cancellationToken)
    {
        var title = await titleGateway.GetById(request.TitleId);
        var season = title.Items.OfType<AnimeSeason>().FirstOrDefault(q => q.Id == request.SeasonId) ??
            throw new Exception("Season not found");
        var episode = season.Episodes.FirstOrDefault(q => q.Id == request.EpisodeId) ??
            throw new Exception("Episode not found");
        if (request.Value) episode.SetCompleted();
        else episode.SetNotCompleted();
        await titleGateway.Update(title);
        return Unit.Value;
    }
}