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

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

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

}

public class AddNameCommandHandler(IAnimeTitleGateway titleGateway, ILanguageGateway languageGateway) : IRequestHandler<AddNameCommand, Unit>
{
    public async Task<Unit> Handle(AddNameCommand 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");
        season.CommonProperties.AddName((Domain.Entities.MediaContent.CommonProperties.NameType)request.NameType, language.Id, request.Value);
        await titleGateway.Update(title);
        return Unit.Value;
    }
}