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

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

public class DeleteNameCommand : IRequest<Unit>
{
    public Guid TitleId { get; set; }
    public Guid SeasonId { get; set; }
    public Guid EpisodeId { get; set; }
    public CommonModels.NameType NameType { get; set; }
    public Guid LanguageId { get; set; }
    public string Value { get; set; } = default!;

}

public class DeleteNameCommandHandler(IAnimeTitleGateway titleGateway, ILanguageGateway languageGateway) : IRequestHandler<DeleteNameCommand, Unit>
{
    public async Task<Unit> Handle(DeleteNameCommand request, CancellationToken cancellationToken)
    {
        var title = await titleGateway.GetById(request.TitleId);
        var language = await languageGateway.GetLanguageById(request.LanguageId);
        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.CommonProperties.RemoveName(new NameItem((NameType)request.NameType, request.LanguageId, request.Value));
        await titleGateway.Update(title);
        return Unit.Value;
    }
}