import { NextResponse } from 'next/server';

interface Check {
  name: string;
  status: 'pass' | 'fail';
  message?: string;
}

const REQUIRED_ENV = ['PLATFORM_API_URL', 'JWT_SECRET', 'DEFAULT_PRODUCT_ID'];

function checkEnvVars(): Check {
  const missing = REQUIRED_ENV.filter(key => !process.env[key]);
  if (missing.length > 0) {
    return { name: 'env', status: 'fail', message: `Missing: ${missing.join(', ')}` };
  }
  return { name: 'env', status: 'pass', message: `${REQUIRED_ENV.length} required vars set` };
}

export async function GET() {
  const checks: Check[] = [];

  checks.push(checkEnvVars());

  const overall = checks.every(c => c.status === 'pass') ? 'ok' : 'degraded';

  return NextResponse.json(
    {
      status: overall,
      service: 'tracker-dashboard',
      timestamp: new Date().toISOString(),
      checks,
    },
    { status: overall === 'ok' ? 200 : 503 }
  );
}
