export function getDeviceInfo() {
  const userAgent = navigator.userAgent;

  // Detect device type
  let deviceType = "Web";
  if (
    /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
      userAgent
    )
  ) {
    deviceType = "Mobile";
  } else if (/Tablet|iPad/i.test(userAgent)) {
    deviceType = "Tablet";
  }

  // Detect OS
  let os = "Unknown";
  let osVersion = "Unknown";
  if (userAgent.indexOf("Windows") !== -1) {
    os = "Windows";
    const match = userAgent.match(/Windows NT (\d+\.\d+)/);
    if (match) {
      const version = parseFloat(match[1]);
      if (version >= 10) {
        osVersion = "11";
      } else if (version >= 6.3) {
        osVersion = "10";
      } else if (version >= 6.2) {
        osVersion = "8.1";
      } else if (version >= 6.1) {
        osVersion = "7";
      }
    }
  } else if (userAgent.indexOf("Mac") !== -1) {
    os = "macOS";
    const match = userAgent.match(/Mac OS X (\d+[._]\d+)/);
    if (match) {
      osVersion = match[1].replace("_", ".");
    }
  } else if (userAgent.indexOf("Linux") !== -1) {
    os = "Linux";
  } else if (userAgent.indexOf("Android") !== -1) {
    os = "Android";
    const match = userAgent.match(/Android (\d+\.\d+)/);
    if (match) {
      osVersion = match[1];
    }
  } else if (
    userAgent.indexOf("iOS") !== -1 ||
    userAgent.indexOf("iPhone") !== -1 ||
    userAgent.indexOf("iPad") !== -1
  ) {
    os = "iOS";
    const match = userAgent.match(/OS (\d+[._]\d+)/);
    if (match) {
      osVersion = match[1].replace("_", ".");
    }
  }

  // Detect browser
  let browser = "Unknown";
  let browserVersion = "Unknown";
  if (
    userAgent.indexOf("Chrome") !== -1 &&
    userAgent.indexOf("Safari") !== -1 &&
    userAgent.indexOf("Edge") === -1
  ) {
    browser = "Chrome";
    const match = userAgent.match(/Chrome\/(\d+\.\d+)/);
    if (match) {
      browserVersion = match[1];
    }
  } else if (userAgent.indexOf("Firefox") !== -1) {
    browser = "Firefox";
    const match = userAgent.match(/Firefox\/(\d+\.\d+)/);
    if (match) {
      browserVersion = match[1];
    }
  } else if (
    userAgent.indexOf("Safari") !== -1 &&
    userAgent.indexOf("Chrome") === -1
  ) {
    browser = "Safari";
    const match = userAgent.match(/Version\/(\d+\.\d+)/);
    if (match) {
      browserVersion = match[1];
    }
  } else if (userAgent.indexOf("Edge") !== -1) {
    browser = "Edge";
    const match = userAgent.match(/Edge\/(\d+\.\d+)/);
    if (match) {
      browserVersion = match[1];
    }
  }

  // Get IP address (this would typically come from server-side)
  // For now, we'll use a placeholder
  const ipAddress = "203.113.5.9"; // This should be obtained from server

  // Get location (this would typically come from server-side or geolocation API)
  // For now, we'll use a placeholder
  const location = "Ho Chi Minh City, Vietnam"; // This should be obtained from server or geolocation

  return {
    deviceType,
    os,
    osVersion,
    browser,
    browserVersion,
    ipAddress,
    location,
  };
}
