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

namespace TakeoutSaaS.TenantApi.Consumers;

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

        // 1. 支付成功 = 新订单出现在待接单列，推送 NewOrder
        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.PaidAt
        }, context.CancellationToken);
    }
}
