import { Controller, Get, SetMetadata } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { IS_PUBLIC_KEY } from '../auth/write-key.guard';
import { RedisRouter } from '../tracking/redis-router.service';

/**
 * Edge health. The collector has no database — readiness means "can we accept
 * an event", and we always can: a black-redis outage spools locally. So health
 * is liveness plus observability into the spool (depth + whether black is
 * currently reachable), which the outage drill reads.
 */
@ApiTags('Health')
@SetMetadata(IS_PUBLIC_KEY, true)
@Controller('health')
export class HealthController {
  constructor(private readonly router: RedisRouter) {}

  @Get()
  @ApiOperation({ summary: 'Health check endpoint' })
  async check() {
    return {
      status: 'ok',
      blackHealthy: this.router.isBlackHealthy(),
      spoolDepth: await this.router.spoolDepth(),
    };
  }

  @Get('live')
  @ApiOperation({ summary: 'Liveness probe' })
  live() {
    return { status: 'ok' };
  }

  @Get('ready')
  @ApiOperation({ summary: 'Readiness probe' })
  ready() {
    // Always ready: the local spool guarantees we can durably accept events
    // even when the canonical store is unreachable.
    return { status: 'ok' };
  }
}
