using MassTransit;
using Tiku.Application.Jobs;
using Tiku.Contracts;

namespace Tiku.Infrastructure.Messaging;

internal sealed class NullBackgroundJobDispatcher : IBackgroundJobDispatcher
{
    public bool IsEnabled => false;

    public Task DispatchAsync(
        Guid jobId,
        Guid tenantId,
        string jobType,
        string correlationId,
        CancellationToken cancellationToken = default) => Task.CompletedTask;
}

internal sealed class MassTransitBackgroundJobDispatcher(IPublishEndpoint publishEndpoint) :
    IBackgroundJobDispatcher
{
    public bool IsEnabled => true;

    public Task DispatchAsync(
        Guid jobId,
        Guid tenantId,
        string jobType,
        string correlationId,
        CancellationToken cancellationToken = default) =>
        publishEndpoint.Publish(
            new BackgroundJobRequestedV1(
                Guid.NewGuid(),
                jobId,
                tenantId,
                jobType,
                DateTimeOffset.UtcNow,
                correlationId),
            context => context.MessageId = jobId,
            cancellationToken);
}
