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

namespace TakeoutSaaS.TenantApi.Consumers;

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

        // 1. 推送状态变更到对应门店 Group
        await hubContext.Clients.Group(group).SendAsync("OrderStatusChanged", new
        {
            e.OrderId,
            e.OrderNo,
            e.OldStatus,
            e.NewStatus,
            e.Channel,
            e.DeliveryType,
            e.CustomerName,
            e.ItemsSummary,
            e.PaidAmount,
            e.OccurredAt
        }, context.CancellationToken);
    }
}
