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

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

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

public class DeleteSeasonCommandHandler(IAnimeTitleGateway titleGateway) : IRequestHandler<DeleteSeasonCommand, Unit>
{
    public async Task<Unit> Handle(DeleteSeasonCommand 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.Delete();
        await titleGateway.Update(title);
        return Unit.Value;
    }
}