using MassTransit;
using Microsoft.Extensions.Logging;
using TakeoutSaaS.Application.Identity.Abstractions;
using TakeoutSaaS.Application.Identity.Events;

namespace TakeoutSaaS.Infrastructure.Logs.Publishers;

/// <summary>
/// 身份模块操作日志发布器（基于 MassTransit Outbox）。
/// </summary>
public sealed class IdentityOperationLogPublisher(
    ILogger<IdentityOperationLogPublisher> logger,
    IPublishEndpoint? publishEndpoint = null) : IIdentityOperationLogPublisher
{
    /// <inheritdoc />
    public Task PublishAsync(IdentityUserOperationLogMessage message, CancellationToken cancellationToken = default)
    {
        if (publishEndpoint is null)
        {
            logger.LogDebug("未配置 MassTransit，已跳过操作日志消息发布：{OperationType}", message.OperationType);
            return Task.CompletedTask;
        }

        // 1. (空行后) 已配置 MassTransit 时正常发布消息
        return publishEndpoint.Publish(message, cancellationToken);
    }
}
