// FE-PAGE-012: Complete Sessions page implementation - User Agent Parser

export interface DeviceInfo {
  deviceType: 'desktop' | 'mobile' | 'tablet' | 'unknown';
  os: string;
  browser: string;
  browserVersion?: string;
  osVersion?: string;
  deviceModel?: string;
}

/**
 * Parse user agent string to extract device information
 */
export function parseUserAgent(userAgent: string): DeviceInfo {
  const ua = userAgent.toLowerCase();
  const info: DeviceInfo = {
    deviceType: 'unknown',
    os: 'Unknown',
    browser: 'Unknown',
  };

  // Detect device type
  if (
    ua.includes('mobile') ||
    ua.includes('android') ||
    ua.includes('iphone')
  ) {
    info.deviceType = 'mobile';
  } else if (ua.includes('tablet') || ua.includes('ipad')) {
    info.deviceType = 'tablet';
  } else {
    info.deviceType = 'desktop';
  }

  // Detect OS
  if (ua.includes('windows')) {
    info.os = 'Windows';
    const match = ua.match(/windows nt (\d+\.\d+)/);
    if (match) {
      const version = match[1];
      if (version === '10.0') info.osVersion = '10';
      else if (version === '6.3') info.osVersion = '8.1';
      else if (version === '6.2') info.osVersion = '8';
      else if (version === '6.1') info.osVersion = '7';
      else info.osVersion = version;
    }
  } else if (ua.includes('mac os x') || ua.includes('macintosh')) {
    info.os = 'macOS';
    const match = ua.match(/mac os x (\d+[._]\d+)/);
    if (match) {
      info.osVersion = match[1].replace('_', '.');
    }
  } else if (ua.includes('linux')) {
    info.os = 'Linux';
  } else if (ua.includes('android')) {
    info.os = 'Android';
    const match = ua.match(/android (\d+\.\d+)/);
    if (match) {
      info.osVersion = match[1];
    }
  } else if (
    ua.includes('ios') ||
    ua.includes('iphone') ||
    ua.includes('ipad')
  ) {
    info.os = 'iOS';
    const match = ua.match(/os (\d+[._]\d+)/);
    if (match) {
      info.osVersion = match[1].replace('_', '.');
    }
  }

  // Detect browser
  if (ua.includes('edg/')) {
    info.browser = 'Microsoft Edge';
    const match = ua.match(/edg\/(\d+\.\d+)/);
    if (match) {
      info.browserVersion = match[1];
    }
  } else if (ua.includes('chrome/') && !ua.includes('edg/')) {
    info.browser = 'Chrome';
    const match = ua.match(/chrome\/(\d+\.\d+)/);
    if (match) {
      info.browserVersion = match[1];
    }
  } else if (ua.includes('firefox/')) {
    info.browser = 'Firefox';
    const match = ua.match(/firefox\/(\d+\.\d+)/);
    if (match) {
      info.browserVersion = match[1];
    }
  } else if (ua.includes('safari/') && !ua.includes('chrome/')) {
    info.browser = 'Safari';
    const match = ua.match(/version\/(\d+\.\d+)/);
    if (match) {
      info.browserVersion = match[1];
    }
  } else if (ua.includes('opera/') || ua.includes('opr/')) {
    info.browser = 'Opera';
    const match = ua.match(/(?:opera|opr)\/(\d+\.\d+)/);
    if (match) {
      info.browserVersion = match[1];
    }
  }

  // Detect device model (for mobile)
  if (ua.includes('iphone')) {
    const match = ua.match(/iphone\s+os\s+(\d+[._]\d+)/);
    if (match) {
      info.deviceModel = 'iPhone';
    }
  } else if (ua.includes('ipad')) {
    info.deviceModel = 'iPad';
  } else if (ua.includes('android')) {
    // Try to extract device model from Android user agent
    const match = ua.match(/android.*?;\s*([^)]+)\)/);
    if (match) {
      info.deviceModel = match[1].trim();
    }
  }

  return info;
}

/**
 * Format device info for display
 */
export function formatDeviceInfo(info: DeviceInfo): string {
  const parts: string[] = [];

  if (info.deviceModel) {
    parts.push(info.deviceModel);
  }

  parts.push(info.os);
  if (info.osVersion) {
    parts.push(info.osVersion);
  }

  parts.push(info.browser);
  if (info.browserVersion) {
    parts.push(info.browserVersion);
  }

  return parts.join(' • ');
}
