import { NextRequest, NextResponse } from 'next/server';
import { requireAdmin } from '@/lib/auth-server';
import { HOST_TOOLS, collectInventoryServices } from '@/lib/ops-stack';

export const dynamic = 'force-dynamic';

export async function GET(req: NextRequest) {
  try {
    const admin = await requireAdmin(req);
    if (!admin) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

    const services = await collectInventoryServices();
    const counts = {
      services: services.length,
      healthy: services.filter(service => service.status === 'healthy').length,
      degraded: services.filter(service => service.status === 'degraded').length,
      down: services.filter(service => service.status === 'down').length,
      hostTools: HOST_TOOLS.length,
    };

    return NextResponse.json({
      timestamp: new Date().toISOString(),
      counts,
      services,
      hostTools: HOST_TOOLS,
    });
  } catch (error) {
    return NextResponse.json(
      { error: error instanceof Error ? error.message : 'Unauthorized' },
      { status: 401 }
    );
  }
}
