/**
 * Device information lookup utilities
 * Provides manufacturer and model information based on serial number patterns
 */

interface DeviceInfo {
  manufacturer: string;
  modelFamily?: string;
  estimatedModel?: string;
}

/**
 * Look up device information based on serial number
 */
export function lookupDeviceBySerial(serialNumber: string | undefined): DeviceInfo | null {
  if (!serialNumber) return null;
  
  const serial = serialNumber.toUpperCase().trim();
  
  // Dell serial numbers (7 characters, alphanumeric)
  if (/^[A-Z0-9]{7}$/.test(serial)) {
    return {
      manufacturer: 'Dell Inc.',
      modelFamily: 'Dell',
      estimatedModel: identifyDellModel(serial)
    };
  }
  
  // HP serial numbers (often start with specific patterns)
  if (/^(CNU|2UA|CND|CNF|MXL|SGH|USE|5CD|5CG)[A-Z0-9]+/.test(serial)) {
    return {
      manufacturer: 'HP',
      modelFamily: 'HP',
      estimatedModel: identifyHPModel(serial)
    };
  }
  
  // Lenovo serial numbers (often start with specific patterns)
  if (/^(MJ|PF|MP|R9|S4|PC|PB)[A-Z0-9]+/.test(serial)) {
    return {
      manufacturer: 'Lenovo',
      modelFamily: 'ThinkPad/ThinkCentre',
      estimatedModel: identifyLenovoModel(serial)
    };
  }
  
  // Apple serial numbers (12 characters)
  if (/^[A-Z0-9]{12}$/.test(serial)) {
    const yearCode = serial.substring(3, 4);
    if (['P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'].includes(yearCode)) {
      return {
        manufacturer: 'Apple Inc.',
        modelFamily: 'Mac',
        estimatedModel: identifyAppleModel(serial)
      };
    }
  }
  
  // Microsoft Surface (often starts with specific patterns)
  if (/^[0-9]{12}$/.test(serial) || serial.startsWith('00')) {
    return {
      manufacturer: 'Microsoft',
      modelFamily: 'Surface',
      estimatedModel: 'Surface Device'
    };
  }
  
  return null;
}

/**
 * Identify Dell model based on service tag patterns
 */
function identifyDellModel(serial: string): string {
  // Dell service tags can indicate model families
  // This is a simplified example - real implementation would need more patterns
  const firstChar = serial[0];
  
  if (['H', 'J', 'K'].includes(firstChar)) {
    return 'OptiPlex Desktop';
  } else if (['F', 'G'].includes(firstChar)) {
    return 'Latitude Laptop';
  } else if (['D', 'C'].includes(firstChar)) {
    return 'Precision Workstation';
  }
  
  return 'Dell Computer';
}

/**
 * Identify HP model based on serial patterns
 */
function identifyHPModel(serial: string): string {
  const prefix = serial.substring(0, 3);
  
  switch (prefix) {
    case 'CNU':
    case 'CND':
      return 'HP ProBook/EliteBook';
    case '2UA':
    case '5CD':
    case '5CG':
      return 'HP Desktop/Workstation';
    case 'MXL':
      return 'HP ProDesk';
    case 'SGH':
      return 'HP Server';
    default:
      return 'HP Computer';
  }
}

/**
 * Identify Lenovo model based on serial patterns
 */
function identifyLenovoModel(serial: string): string {
  const prefix = serial.substring(0, 2);
  
  switch (prefix) {
    case 'MJ':
      return 'ThinkCentre Desktop';
    case 'PF':
    case 'PC':
    case 'PB':
      return 'ThinkPad Laptop';
    case 'MP':
      return 'ThinkStation';
    case 'R9':
    case 'S4':
      return 'IdeaPad/Yoga';
    default:
      return 'Lenovo Computer';
  }
}

/**
 * Identify Apple model based on serial number
 */
function identifyAppleModel(serial: string): string {
  // Apple serial numbers encode model info in positions 4-5
  const modelCode = serial.substring(4, 6);
  
  // This is a simplified mapping - real implementation would need comprehensive list
  if (modelCode.startsWith('M')) {
    return 'MacBook Pro';
  } else if (modelCode.startsWith('F')) {
    return 'MacBook Air';
  } else if (modelCode.startsWith('G')) {
    return 'iMac';
  } else if (modelCode.startsWith('J')) {
    return 'Mac mini';
  } else if (modelCode.startsWith('P')) {
    return 'Mac Studio/Pro';
  }
  
  return 'Mac Computer';
}

/**
 * Format device information for display
 */
export function formatDeviceInfo(info: DeviceInfo | null): string {
  if (!info) return 'Unknown Device';
  
  if (info.estimatedModel) {
    return `${info.manufacturer} - ${info.estimatedModel}`;
  }
  
  if (info.modelFamily) {
    return `${info.manufacturer} ${info.modelFamily}`;
  }
  
  return info.manufacturer;
}
