/**
 * Intelligent User-Agent Parser for Admin Multi-Device Session Management.
 * Extracts clean, human-readable browser, version, operating system, and device type.
 */

export interface ParsedDeviceInfo {
  browser: string;
  browserVersion: string | null;
  operatingSystem: string;
  deviceType: "Desktop" | "Mobile" | "Tablet" | "Unknown";
  summary: string;
}

export function parseUserAgent(ua: string | null | undefined): ParsedDeviceInfo {
  if (!ua || typeof ua !== "string") {
    return {
      browser: "Unknown Browser",
      browserVersion: null,
      operatingSystem: "Unknown OS",
      deviceType: "Unknown",
      summary: "Unknown Device",
    };
  }

  // 1. Device Type
  let deviceType: "Desktop" | "Mobile" | "Tablet" | "Unknown" = "Desktop";
  if (/ipad|tablet|(android(?!.*mobile))/i.test(ua)) {
    deviceType = "Tablet";
  } else if (/mobile|iphone|ipod|android.*mobile|blackberry|windows phone/i.test(ua)) {
    deviceType = "Mobile";
  }

  // 2. Operating System
  let operatingSystem = "Unknown OS";
  if (/windows nt 10\.0/i.test(ua)) {
    // Windows 10 and 11 share NT 10.0; check for Win11 indicators if available
    operatingSystem = "Windows 11 / 10";
  } else if (/windows nt 6\.3/i.test(ua)) {
    operatingSystem = "Windows 8.1";
  } else if (/windows nt 6\.1/i.test(ua)) {
    operatingSystem = "Windows 7";
  } else if (/windows/i.test(ua)) {
    operatingSystem = "Windows";
  } else if (/iphone os ([0-9_]+)/i.test(ua)) {
    const match = ua.match(/iphone os ([0-9_]+)/i);
    const version = match ? match[1].replace(/_/g, ".") : "";
    operatingSystem = version ? `iOS ${version}` : "iOS";
  } else if (/ipad.*os ([0-9_]+)/i.test(ua)) {
    const match = ua.match(/os ([0-9_]+)/i);
    const version = match ? match[1].replace(/_/g, ".") : "";
    operatingSystem = version ? `iPadOS ${version}` : "iPadOS";
  } else if (/mac os x ([0-9_]+)/i.test(ua)) {
    const match = ua.match(/mac os x ([0-9_]+)/i);
    const version = match ? match[1].replace(/_/g, ".") : "";
    operatingSystem = version ? `macOS ${version}` : "macOS";
  } else if (/android ([0-9.]+)/i.test(ua)) {
    const match = ua.match(/android ([0-9.]+)/i);
    operatingSystem = match ? `Android ${match[1]}` : "Android";
  } else if (/cros/i.test(ua)) {
    operatingSystem = "ChromeOS";
  } else if (/linux/i.test(ua)) {
    operatingSystem = "Linux";
  }

  // 3. Browser & Major Version
  let browser = "Web Browser";
  let browserVersion: string | null = null;

  if (/edg\/([0-9.]+)/i.test(ua)) {
    browser = "Edge";
    browserVersion = ua.match(/edg\/([0-9.]+)/i)?.[1]?.split(".")[0] || null;
  } else if (/opr\/([0-9.]+)/i.test(ua) || /opera/i.test(ua)) {
    browser = "Opera";
    browserVersion = ua.match(/opr\/([0-9.]+)/i)?.[1]?.split(".")[0] || null;
  } else if (/chrome\/([0-9.]+)/i.test(ua) || /crios\/([0-9.]+)/i.test(ua)) {
    browser = "Chrome";
    browserVersion = (ua.match(/chrome\/([0-9.]+)/i) || ua.match(/crios\/([0-9.]+)/i))?.[1]?.split(".")[0] || null;
  } else if (/firefox\/([0-9.]+)/i.test(ua) || /fxios\/([0-9.]+)/i.test(ua)) {
    browser = "Firefox";
    browserVersion = (ua.match(/firefox\/([0-9.]+)/i) || ua.match(/fxios\/([0-9.]+)/i))?.[1]?.split(".")[0] || null;
  } else if (/version\/([0-9.]+).*safari/i.test(ua)) {
    browser = "Safari";
    browserVersion = ua.match(/version\/([0-9.]+)/i)?.[1]?.split(".")[0] || null;
  } else if (/safari/i.test(ua)) {
    browser = "Safari";
  }

  const versionString = browserVersion ? ` ${browserVersion}` : "";
  const summary = `${browser}${versionString} on ${operatingSystem} (${deviceType})`;

  return {
    browser,
    browserVersion,
    operatingSystem,
    deviceType,
    summary,
  };
}
