// iam gRPC 健康探针（/readyz P2 必需依赖）
// 调用 grpc.health.v1.Health/Check 检查 iam:50052 是否 SERVING
import { Injectable, Inject } from "@nestjs/common";
import { IAM_CLIENT } from "../../clients/iam/iam-client.interface.js";
import type { IamClient } from "../../clients/iam/iam-client.interface.js";
import type { HealthProbe } from "./readiness.probe.js";

@Injectable()
export class IamGrpcHealthProbe implements HealthProbe {
  readonly name = "iam-grpc";
  readonly required = true;

  constructor(@Inject(IAM_CLIENT) private readonly iam: IamClient) {}

  async check(): Promise<{
    status: "up" | "down";
    latencyMs: number;
    error?: string;
  }> {
    const result = await this.iam.checkHealth();
    return {
      status: result.serving ? "up" : "down",
      latencyMs: result.latencyMs,
      error: result.error,
    };
  }
}
