namespace HrynCo.NotificationService.Worker.Services.EmailProcessing; using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.DAL.Abstract.Templates; internal sealed class EmailTemplateService : IEmailTemplateService { private readonly IEmailTemplateRepository _templateRepository; public EmailTemplateService(IEmailTemplateRepository templateRepository) { _templateRepository = templateRepository; } public async Task GetAsync( string serviceName, string templateKey, string? languageCode, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(languageCode)) throw new InvalidDataException("LanguageCode is required."); EmailTemplate? template = await _templateRepository.GetAsync( serviceName, templateKey, languageCode, cancellationToken); if (template is null) { string compatibilityCode = languageCode.Trim().ToLowerInvariant(); if (!string.Equals(languageCode, compatibilityCode, StringComparison.Ordinal)) template = await _templateRepository.GetAsync(serviceName, templateKey, compatibilityCode, cancellationToken); } return template ?? throw new InvalidOperationException( $"Template not found: service='{serviceName}' key='{templateKey}' language='{languageCode}'."); } }