using JSMR.Application.Tags.Ports;

namespace JSMR.Application.Tags.Commands.SetEnglishName;

public sealed class SetTagEnglishNameHandler(ITagWriter tagWriter)
{
    public async Task<SetTagEnglishNameResponse> HandleAsync(SetTagEnglishNameRequest request, CancellationToken cancellationToken = default)
    {
        if (string.IsNullOrWhiteSpace(request.EnglishName))
            throw new ArgumentException("English name is required.", nameof(request));

        string trimmed = request.EnglishName.Trim();
        SetTagEnglishNameResponse result = await tagWriter.SetEnglishNameAsync(request with { EnglishName = trimmed }, cancellationToken);

        // Optional: refresh search materialization for all voice works that use this tag
        //await _searchUpdater.UpdateForTagAsync(request.TagId, cancellationToken);

        return result;
    }
}