import { Controller, Get } from '@nestjs/common';
import {
  HealthCheck,
  HealthCheckResult,
  HealthCheckService,
  MemoryHealthIndicator,
} from '@nestjs/terminus';
import { Public } from '../auth/public.decorator';
import { WorkspacePoolHealthIndicator } from './workspace-pool.health-indicator';

// CODING-RULES §20 (PGD-OBS-003):
//   /health/live  → liveness: memory.checkHeap(350MB).
//   /health/ready → readiness: heap + amostra LRU (K=3) do WorkspacePrismaPool.
//   Próximos: MasterLoginHealthIndicator, ValkeyHealthIndicator, BullMQHealthIndicator.
//   NUNCA percorrer todos os workspaces (O(N)).

@Public()
@Controller({ path: 'health' })
export class HealthController {
  constructor(
    private readonly health: HealthCheckService,
    private readonly memory: MemoryHealthIndicator,
    private readonly workspacePool: WorkspacePoolHealthIndicator,
  ) {}

  @Get('live')
  @HealthCheck()
  live(): Promise<HealthCheckResult> {
    return this.health.check([() => this.memory.checkHeap('heap', 350 * 1024 * 1024)]);
  }

  @Get('ready')
  @HealthCheck()
  ready(): Promise<HealthCheckResult> {
    return this.health.check([
      () => this.memory.checkHeap('heap', 350 * 1024 * 1024),
      () => this.workspacePool.check('workspace_pool', 3),
    ]);
  }
}
