import { Controller, Get } from '@nestjs/common';
import { PrismaService } from '../../common/prisma/prisma.service';
import { Public } from '../../common/decorators/auth.decorators';

@Controller('health')
export class HealthController {
  constructor(private readonly prisma: PrismaService) {}

  @Public()
  @Get()
  async live(): Promise<{ status: string; ts: string }> {
    return { status: 'ok', ts: new Date().toISOString() };
  }

  @Public()
  @Get('ready')
  async ready(): Promise<{ status: string; db: string; ts: string }> {
    let dbStatus = 'down';
    try {
      await this.prisma.$queryRaw`SELECT 1`;
      dbStatus = 'up';
    } catch {
      // dbStatus stays "down"
    }
    return {
      status: dbStatus === 'up' ? 'ok' : 'degraded',
      db: dbStatus,
      ts: new Date().toISOString(),
    };
  }
}
