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

namespace TakeoutSaaS.TenantApi.Consumers;

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

        // 1. 推送新订单到对应门店 Group
        await hubContext.Clients.Group(group).SendAsync("NewOrder", new
        {
            e.OrderId,
            e.OrderNo,
            e.Amount,
            e.StoreId,
            e.Channel,
            e.DeliveryType,
            e.CustomerName,
            e.ItemsSummary,
            e.TableNo,
            e.CreatedAt
        }, context.CancellationToken);
    }
}
