import type { APIRoute } from 'astro';

const startTime = Date.now();

export const GET: APIRoute = async () => {
  const uptimeSeconds = Math.floor((Date.now() - startTime) / 1000);
  const memUsage = process.memoryUsage();

  const payload = {
    status: 'ok',
    timestamp: new Date().toISOString(),
    uptime: uptimeSeconds,
    version: import.meta.env.npm_package_version ?? '1.0.0',
    mode: 'ssr',
    adapter: 'node-standalone',
    domain: 'workroot.in',
    memory: {
      heapUsedMB: Math.round(memUsage.heapUsed / 1024 / 1024),
      heapTotalMB: Math.round(memUsage.heapTotal / 1024 / 1024),
      rssMB: Math.round(memUsage.rss / 1024 / 1024),
    },
    checks: {
      server: 'ok',
    },
  };

  return new Response(JSON.stringify(payload), {
    status: 200,
    headers: {
      'Content-Type': 'application/json',
      'Cache-Control': 'no-cache, no-store, must-revalidate',
      'Access-Control-Allow-Origin': 'https://workroot.in',
      'Access-Control-Allow-Methods': 'GET',
      'Access-Control-Allow-Headers': 'Content-Type',
    },
  });
};
