import { Controller, Get, HttpCode, Res } from '@nestjs/common';
import { Response } from 'express';
import { HealthService } from './health.service';
import { Public } from '../auth/decorators/public.decorator';

@Public()
@Controller('health')
export class HealthController {
  constructor(private readonly healthService: HealthService) {}

  /**
   * Övergripande hälsostatus för tjänsten
   * Returnerar 200 om allt är bra, 503 om något är fel
   */
  @Get()
  async getHealth(@Res() res: Response) {
    const health = await this.healthService.getOverallHealth();
    res.status(health.statusCode).json({
      status: health.status,
      service: health.service,
      timestamp: health.timestamp,
      uptime: health.uptime,
      checks: health.checks,
    });
  }

  /**
   * Databasspecific hälsokontroll
   * Returnerar 200 om databasen är tillgänglig, 503 om inte
   */
  @Get('db')
  async getDatabaseHealth(@Res() res: Response) {
    const dbHealth = await this.healthService.getDatabaseHealth();
    res.status(dbHealth.statusCode).json({
      status: dbHealth.status,
      database: dbHealth.database,
      responseTime: `${dbHealth.responseTime}ms`,
      timestamp: dbHealth.timestamp,
      details: dbHealth.details,
    });
  }
}

