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

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

public class EditNameCommand : IRequest<Unit>
{
    public Guid TitleId { get; set; }
    public CommonModels.NameType NameType { get; set; }
    public Guid LanguageId { get; set; }
    public string Value { get; set; } = default!;
    public Guid? NewLanguageId { get; set; }
    public string? NewValue { get; set; } = default!;
}

public class EditNameCommandHandler(IAnimeTitleGateway titleGateway) : IRequestHandler<EditNameCommand, Unit>
{
    public async Task<Unit> Handle(EditNameCommand request, CancellationToken cancellationToken)
    {
        if (!request.NewLanguageId.HasValue && string.IsNullOrWhiteSpace(request.NewValue))
        {
            throw new ArgumentNullException($"{nameof(EditNameCommand.NewLanguageId)}, {nameof(EditNameCommand.NewValue)}");
        }
        var title = await titleGateway.GetById(request.TitleId);
        title.CommonProperties.SetNameValue(new NameItem((NameType)request.NameType, request.LanguageId, request.Value), request.NewLanguageId, request.NewValue);
        await titleGateway.Update(title);
        return Unit.Value;
    }
}