import { Body, Controller, Get, Post } from '@nestjs/common';

const startedAt = new Date();

@Controller()
export class AppController {
  @Get()
  info() {
    const mem = process.memoryUsage();
    return {
      status: 'ok',
      message: 'NestJS jalan di shared hosting 🎉',
      node: process.version,
      env: process.env.NODE_ENV || '(tidak diset)',
      pid: process.pid,
      startedAt: startedAt.toISOString(),
      uptimeSec: Math.round(process.uptime()),
      memoryMB: {
        rss: +(mem.rss / 1048576).toFixed(1),
        heapUsed: +(mem.heapUsed / 1048576).toFixed(1),
      },
      runBy:
        typeof (globalThis as any).PhusionPassenger !== 'undefined' || process.env.PASSENGER_APP_ENV
          ? 'passenger'
          : 'manual/lokal',
    };
  }

  @Get('health')
  health() {
    return 'OK';
  }

  @Post('echo')
  echo(@Body() body: unknown) {
    return { youSent: body };
  }
}
