/** {{ module_title }} module health indicator for NestJS.
 *  Generated by RapidKit.
 */

import {
  Controller,
  Get,
  HttpCode,
  HttpStatus,
  Logger,
  ServiceUnavailableException,
} from "@nestjs/common";

import { {{ module_class_name }}Service } from "./{{ module_kebab }}.service";

interface HealthPayload {
  status: string;
  totals: Record<string, number>;
  issues: string[];
  metadata: Record<string, unknown>;
}

@Controller("api/health/module")
export class {{ module_class_name }}HealthController {
  private readonly logger = new Logger({{ module_class_name }}HealthController.name);

  constructor(private readonly service: {{ module_class_name }}Service) {}

  @Get("{{ module_kebab }}")
  @HttpCode(HttpStatus.OK)
  getModuleHealth(): HealthPayload {
    try {
      const payload = this.service.getHealth();
      this.logger.debug(
        `{{ module_title }} health payload=${JSON.stringify(payload)}`,
      );
      return payload as HealthPayload;
    } catch (error) {
      const detail =
        error instanceof Error && error.message ? error.message : "{{ module_title }} health check failed";
      this.logger.error(`{{ module_title }} health check failed: ${detail}`);
      throw new ServiceUnavailableException({
        status: "error",
        module: "{{ module_slug }}",
        detail,
      });
    }
  }
}
