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

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

public class SetGenreProportionCommand : IRequest<Unit>
{
    public Guid TitleId { get; set; }
    public Guid SeasonId { get; set; }
    public Guid GenreId { get; set; }
    public decimal? Proportion { get; set; }
    public decimal? NewProportion { get; set; }
}

public class SetGenreProportionCommandHandler(IAnimeTitleGateway titleGateway) : IRequestHandler<SetGenreProportionCommand, Unit>
{
    public async Task<Unit> Handle(SetGenreProportionCommand 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");
        season.CommonProperties.SetGenreProportion(request.GenreId, request.NewProportion);
        await titleGateway.Update(title);
        return Unit.Value;
    }
}