using MediatR;
using Modules.Library.Application.Gateways;

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

public class AddDescriptionCommand : IRequest<Unit>
{
    public Guid TitleId { get; set; }
    public Guid LanguageId { get; set; }
    public string Value { get; set; } = default!;
    public bool IsOriginal { get; set; }

}

public class AddDescriptionCommandHandler(IAnimeTitleGateway titleGateway, ILanguageGateway languageGateway) : IRequestHandler<AddDescriptionCommand, Unit>
{
    public async Task<Unit> Handle(AddDescriptionCommand request, CancellationToken cancellationToken)
    {
        var title = await titleGateway.GetById(request.TitleId);
        if (!await languageGateway.IsLanguageExists(request.LanguageId)) throw new Exception();
        if (request.IsOriginal) title.CommonProperties.AddOriginalDescription(request.LanguageId, request.Value);
        else title.CommonProperties.AddNotOriginalDescription(request.LanguageId, request.Value);
        await titleGateway.Update(title);
        return Unit.Value;
    }
}