/**
 * Device Detection Utility
 * Parses User-Agent and other request headers to detect device type and information
 */

export interface DeviceInfo {
  type: 'mobile' | 'tablet' | 'desktop' | 'unknown';
  os: string;
  browser: string;
  device: string;
  userAgent: string;
}

export class DeviceDetection {
  /**
   * Detect device information from request headers
   */
  public static detectDevice(userAgent: string): DeviceInfo {
    const ua = userAgent.toLowerCase();

    // Detect device type
    const type = this.detectDeviceType(ua);

    // Detect OS
    const os = this.detectOS(ua);

    // Detect browser
    const browser = this.detectBrowser(ua);

    // Detect device name
    const device = this.detectDeviceName(ua);

    return {
      type,
      os,
      browser,
      device,
      userAgent,
    };
  }

  /**
   * Detect device type (mobile, tablet, desktop)
   */
  private static detectDeviceType(
    ua: string
  ): 'mobile' | 'tablet' | 'desktop' | 'unknown' {
    // Check for tablet first (more specific)
    if (/(ipad|tablet|playbook|silk)|(android(?!.*mobile))/i.test(ua)) {
      return 'tablet';
    }

    // Check for mobile
    if (
      /mobile|iphone|ipod|android|blackberry|opera mini|opera mobi|skyfire|maemo|windows phone|palm|iemobile|symbian|symbianos|fennec/i.test(
        ua
      )
    ) {
      return 'mobile';
    }

    // Check for desktop
    if (/windows|macintosh|linux|x11/i.test(ua)) {
      return 'desktop';
    }

    return 'unknown';
  }

  /**
   * Detect operating system
   */
  private static detectOS(ua: string): string {
    if (/windows phone/i.test(ua)) return 'Windows Phone';
    if (/windows/i.test(ua)) return 'Windows';
    if (/android/i.test(ua)) return 'Android';
    if (/ipad|iphone|ipod/i.test(ua)) return 'iOS';
    if (/mac os x/i.test(ua)) return 'macOS';
    if (/linux/i.test(ua)) return 'Linux';
    if (/cros/i.test(ua)) return 'Chrome OS';
    return 'Unknown OS';
  }

  /**
   * Detect browser
   */
  private static detectBrowser(ua: string): string {
    // Check Chromium-based browsers before Chrome (they all contain "chrome" in UA)
    if (/brave/i.test(ua)) return 'Brave';
    if (/opr\/|opera/i.test(ua)) return 'Opera';
    if (/edg/i.test(ua)) return 'Edge';
    if (/chrome|crios|crmo/i.test(ua)) return 'Chrome';
    if (/firefox|fxios/i.test(ua)) return 'Firefox';
    if (/safari/i.test(ua) && !/chrome/i.test(ua)) return 'Safari';
    if (/msie|trident/i.test(ua)) return 'Internet Explorer';
    return 'Unknown Browser';
  }

  /**
   * Detect device name/model
   */
  private static detectDeviceName(ua: string): string {
    // iPhone models
    if (/iphone/i.test(ua)) {
      if (/iphone\s*(\d+[,_]\d+)/i.test(ua)) {
        const match = ua.match(/iphone\s*(\d+[,_]\d+)/i);
        return match ? `iPhone ${match[1].replace(/[,_]/g, '.')}` : 'iPhone';
      }
      return 'iPhone';
    }

    // iPad models
    if (/ipad/i.test(ua)) return 'iPad';

    // Android devices
    if (/android/i.test(ua)) {
      // Try to extract device model
      const androidMatch = ua.match(/android.*;\s*([^)]+)\s*build/i);
      if (androidMatch && androidMatch[1]) {
        return androidMatch[1].trim();
      }
      return 'Android Device';
    }

    // Windows devices
    if (/windows phone/i.test(ua)) return 'Windows Phone';
    if (/windows/i.test(ua)) return 'Windows PC';

    // Mac devices
    if (/macintosh|mac os x/i.test(ua)) return 'Mac';

    // Linux
    if (/linux/i.test(ua)) return 'Linux PC';

    return 'Unknown Device';
  }

  /**
   * Format device info as a readable string
   */
  public static formatDeviceInfo(deviceInfo: DeviceInfo): string {
    return `${deviceInfo.device} (${deviceInfo.os}) - ${deviceInfo.browser}`;
  }
}
