import dns from "node:dns";
import { performance } from "node:perf_hooks";

interface ProbeResult {
  status: boolean;
  ip: string;
  responseTime: number;
}

/**
 * Fetch network information for a specific URL
 * Includes (ip, response time, status)
 */
export default async function probe(url: string): Promise<ProbeResult> {
  try {
    const domain = url.replace("https://", "");

    const [ip, status] = await Promise.all([
      getIp(domain),
      fetch(url).then((r) => r.ok),
    ]);

    const responseTime = Math.round(performance.now() - start);

    return { ip, status, responseTime, url, domain };
  } catch (e) {
    console.log(e);
  }
}

async function getIp(domain: string): Promise<string | null> {
  try {
    const response = await dns.promises.resolve(domain);

    if (response.length) {
      return response[0]!;
    }

    return null;
  } catch (e) {
    return null;
  }
}

async function getStatus(url: string) {}
