import { sendPush } from "../shared/push/push-gateway.client.js";
import type {
  ChannelSendContext,
  ChannelSendResult,
  NotificationChannelStrategy,
} from "./channel.types.js";

/**
 * PushChannel —— 移动推送渠道。
 *
 * 通过 push-gateway HTTP /internal/push 推送。
 * 仲裁依据 M4：HTTP POST（豁免 gRPC）。
 *
 * 软失败：push-gateway 不可用或用户离线时返回 sent=false，不阻断。
 */
class PushChannel implements NotificationChannelStrategy {
  readonly name = "push" as const;

  async send(ctx: ChannelSendContext): Promise<ChannelSendResult> {
    const result = await sendPush({
      userId: ctx.userId,
      event: "notification.push",
      data: {
        id: ctx.notificationId,
        type: ctx.type,
        title: ctx.title,
        content: ctx.content,
        metadata: ctx.metadata,
      },
    });

    return {
      channel: "push",
      sent: result.sent,
      error: result.error,
    };
  }
}

export const pushChannel = new PushChannel();
