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

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

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

public class DeleteSeasonCommandHandler(IAnimeTitleGateway titleGateway, IAnimeSeasonGateway seasonGateway) : 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>().First(q => q.Id == request.SeasonId);
        try
        {
            season.Delete();
            await titleGateway.Update(title);
            if (request.DisableSoftDelete)
            {
                await seasonGateway.Delete(title.Id, season);
            }
            else
            {
                await seasonGateway.Update(title.Id, season);
            }
        }
        catch(Exception ex) { }
        return Unit.Value;
    }
}