using Microsoft.Extensions.Logging;
using PARR.Core.Common.Interfaces.RabbitServices;
using PARR.Domain.Common.Rabbit.Messages.TemplateMatching;
using PARR.TemplateMatcher.Services.Interfaces;
using PARR.TemplateMatcher.Settings;

namespace PARR.TemplateMatcher.Services.Implementations
{
    internal class TemplateMqPublisher : ITemplateMqPublisher
    {
        private readonly ILogger<TemplateMqPublisher> logger;
        private readonly IRabbitService mqService;
        private readonly MqSettings mqSettings;

        public TemplateMqPublisher(ILogger<TemplateMqPublisher> logger, IRabbitService mqService, MqSettings mqSettings)
        {
            this.logger = logger;
            this.mqService = mqService;
            this.mqSettings = mqSettings;
        }

        public async Task PublishUpdateAsync(TemplateUpdaterMessage message, CancellationToken ct = default)
        {
            var result = await mqService.SendAsync(mqSettings.TemplateUpdater, new List<object> { message });
            if (!result.IsSuccess)
                logger.LogError("Ошибка публикации команды обновления шаблона {TemplateId}.", message.TemplateId);
        }

        public async Task PublishCreateAsync(TemplateGeneratorMessage message, CancellationToken ct = default)
        {
            var result = await mqService.SendAsync(mqSettings.TemplateGenerator, new List<object> { message });
            if (!result.IsSuccess)
                logger.LogError("Ошибка публикации команды создания шаблона для Job {JobId}, Unit {UnitId}.", message.JobId, message.UnitId);
        }
    }
}
