// 健康检查端点（G3 + president §2.4）
// - GET /healthz：liveness，仅返回进程存活（不检查下游）
// - GET /readyz：readiness，DownstreamHealthCheck 注册表（P2: Redis + iam gRPC）
//
// 软失败规则（contract §3.3）：
//   - status=ok → 200
//   - status=degraded → 200（可选依赖失败，告警但不阻断）
//   - status=unavailable → 503（必需依赖失败）
import { Controller, Get, Res, HttpStatus } from "@nestjs/common";
import type { Response } from "express";
import { healthRegistry } from "./readiness.probe.js";

const SERVICE_NAME = "teacher-bff";

@Controller()
export class HealthController {
  @Get("healthz")
  liveness(): { status: string; service: string; timestamp: string } {
    return {
      status: "ok",
      service: SERVICE_NAME,
      timestamp: new Date().toISOString(),
    };
  }

  @Get("readyz")
  async readiness(@Res() res: Response): Promise<void> {
    const report = await healthRegistry.runAll();
    const statusCode =
      report.status === "unavailable"
        ? HttpStatus.SERVICE_UNAVAILABLE
        : HttpStatus.OK;
    res.status(statusCode).json(report);
  }
}
