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 SetDurationCommand : IRequest<Unit>
{
    public Guid TitleId { get; init; }
    public Guid SeasonId { get; init; }
    public Guid EpisodeId { get; init; }
    public TimeSpan Duration { get; init; }

}

public class SetDurationCommandHandler(IAnimeTitleGateway titleGateway) : IRequestHandler<SetDurationCommand, Unit>
{
    public async Task<Unit> Handle(SetDurationCommand 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");
        episode.SetDuration(request.Duration);
        await titleGateway.Update(title);
        return Unit.Value;
    }
}