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

import {
  DB_SQLITE_VENDOR_MODULE,
  type {{ module_class_name }}HealthPayload,
  type {{ module_class_name }}TableInfo,
  {{ module_class_name }}Service,
} from './{{ module_kebab }}.service';

@Controller('{{ module_kebab }}')
export class {{ module_class_name }}Controller {
  private readonly logger = new Logger({{ module_class_name }}Controller.name);

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

  @Get('health')
  @HttpCode(HttpStatus.OK)
  getHealth(): {{ module_class_name }}HealthPayload {
    try {
      const payload = this.service.getHealthPayload();
      this.logger.debug(`SQLite health payload=${JSON.stringify(payload)}`);
      return payload;
    } catch (error) {
      const detail =
        error instanceof Error && error.message ? error.message : String(error);
      this.logger.error(`SQLite health check failed: ${detail}`);
      throw new ServiceUnavailableException({
        status: 'error',
        module: DB_SQLITE_VENDOR_MODULE,
        detail,
      });
    }
  }

  @Get('tables')
  listTables(): {{ module_class_name }}TableInfo[] {
    return this.service.listTables();
  }
}
