/** Settings module health indicator for NestJS.
 *
 * This file is generated by RapidKit's settings module.
 */

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

import { SETTINGS_VENDOR_MODULE } from "../modules/free/essentials/settings/configuration";
import {
  SettingsHealthPayload,
  SettingsService,
} from "../modules/free/essentials/settings/settings.service";

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

  constructor(private readonly service: SettingsService) {}

  @Get("settings")
  @HttpCode(HttpStatus.OK)
  getSettingsHealth(): SettingsHealthPayload {
    try {
      const payload = this.service.getHealth();
      this.logger.debug(
        `Settings health endpoint invoked payload=${JSON.stringify(payload)}`,
      );
      return payload;
    } catch (error) {
      const message =
        error instanceof Error && error.message
          ? error.message
          : "settings health check failed";
      const detail = typeof error === "string" ? error : message;
      this.logger.error(`Settings health check failed: ${detail}`);
      throw new ServiceUnavailableException({
        status: "error",
        module: SETTINGS_VENDOR_MODULE,
        detail: message,
      });
    }
  }
}
