import { NextResponse } from 'next/server';
import os from 'os';

export async function GET() {
  const interfaces = os.networkInterfaces();
  let lanIp = '127.0.0.1';

  // Find the primary active external IPv4 address
  for (const name of Object.keys(interfaces)) {
    const ifaceList = interfaces[name];
    if (!ifaceList) continue;
    for (const net of ifaceList) {
      // Filter for IPv4 and non-internal addresses (e.g. en0 Wi-Fi, en1 Ethernet)
      if (net.family === 'IPv4' && !net.internal) {
        lanIp = net.address;
        break;
      }
    }
    if (lanIp !== '127.0.0.1') break;
  }

  return NextResponse.json({
    lanIp,
    status: 'online',
    timestamp: new Date().toISOString()
  });
}
