using MassTransit;
using Microsoft.AspNetCore.SignalR;
using TakeoutSaaS.Application.Messaging.Events;
using TakeoutSaaS.TenantApi.Hubs;

namespace TakeoutSaaS.TenantApi.Consumers;

/// <summary>
/// 订单催单事件消费者 — 推送催单通知到看板。
/// </summary>
public sealed class OrderUrgeConsumer(IHubContext<OrderBoardHub> hubContext)
    : IConsumer<OrderUrgeEvent>
{
    /// <inheritdoc />
    public async Task Consume(ConsumeContext<OrderUrgeEvent> context)
    {
        var e = context.Message;
        var group = $"store:{e.TenantId}:{e.StoreId}";

        // 1. 推送催单通知到对应门店 Group
        await hubContext.Clients.Group(group).SendAsync("OrderUrged", new
        {
            e.OrderId,
            e.OrderNo,
            e.UrgeCount,
            e.OccurredAt
        }, context.CancellationToken);
    }
}
