export interface VendorOidProfile {
  vendor: 'VSOL' | 'HUAWEI' | 'BDCOM' | 'ZTE';
  rxPowerOid: string;
  txPowerOid: string;
  unregisteredOnuOid: string;
  cliCommandToDiscover: string;
}

export const VENDOR_PROFILES: Record<string, VendorOidProfile> = {
  VSOL: {
    vendor: 'VSOL',
    rxPowerOid: '1.3.6.1.4.1.37950.1.1.5.12.2.1.8', // VSOL Optical RX
    txPowerOid: '1.3.6.1.4.1.37950.1.1.5.12.2.1.7',
    unregisteredOnuOid: '1.3.6.1.4.1.37950.1.1.5.10.1.1',
    cliCommandToDiscover: 'show gpon onu uncfg'
  },
  HUAWEI: {
    vendor: 'HUAWEI',
    rxPowerOid: '1.3.6.1.4.1.2011.6.128.1.1.2.51.1.4', // Huawei Optical RX
    txPowerOid: '1.3.6.1.4.1.2011.6.128.1.1.2.51.1.3',
    unregisteredOnuOid: '1.3.6.1.4.1.2011.6.128.1.1.2.21.1',
    cliCommandToDiscover: 'display ont autofind all'
  },
  BDCOM: {
    vendor: 'BDCOM',
    rxPowerOid: '1.3.6.1.4.1.3320.10.3.4.1.2',
    txPowerOid: '1.3.6.1.4.1.3320.10.3.4.1.1',
    unregisteredOnuOid: '1.3.6.1.4.1.3320.10.3.1.1',
    cliCommandToDiscover: 'show epon unauth-onu'
  },
  ZTE: {
    vendor: 'ZTE',
    rxPowerOid: '1.3.6.1.4.1.3902.1012.3.50.12.1.1.14',
    txPowerOid: '1.3.6.1.4.1.3902.1012.3.50.12.1.1.13',
    unregisteredOnuOid: '1.3.6.1.4.1.3902.1012.3.10.1.1',
    cliCommandToDiscover: 'show gpon onu uncfg'
  }
};

export class SnmpScraperEngine {
  /**
   * Evaluates raw optical RX power against strict ISP color boundary metrics:
   * 🟢 -8 dBm to -24 dBm = Healthy (Good Signal)
   * 🟡 -24.1 dBm to -27 dBm = Warning (Trigger automated log metrics)
   * 🔴 > -27.1 dBm / LOS = Critical Line Fault (Auto ticket + Push Alert)
   */
  public static calculateLaserHealth(rxPowerDbm: number): {
    status: 'healthy' | 'warning' | 'critical_fault';
    colorCode: 'GREEN' | 'YELLOW' | 'RED';
    label: string;
  } {
    // Optical power in dBm is negative: -8 is stronger than -24, which is stronger than -27.1
    // A power of -28 dBm is worse than -27.1 dBm (i.e. attenuation is higher / signal weaker)
    if (rxPowerDbm >= -24.0 && rxPowerDbm <= -8.0) {
      return {
        status: 'healthy',
        colorCode: 'GREEN',
        label: 'Healthy (Good Optical Signal)'
      };
    } else if (rxPowerDbm < -24.0 && rxPowerDbm >= -27.0) {
      return {
        status: 'warning',
        colorCode: 'YELLOW',
        label: 'Warning (High Fiber Attenuation / Minor Bend)'
      };
    } else {
      // More negative than -27.0 dBm (e.g. -27.1, -29, -35) or > -8 dBm (optical overload/burn) or LOS
      return {
        status: 'critical_fault',
        colorCode: 'RED',
        label: 'Critical Line Fault (LOS / Severe Fiber Cut)'
      };
    }
  }

  /**
   * Simulates SNMP v2c / SSH CLI polling on physical OLT hardware
   */
  public static async pollOltOpticalMetrics(oltIp: string, vendor: 'VSOL' | 'HUAWEI' | 'BDCOM' | 'ZTE', snmpCommunity: string): Promise<Array<{ macSerial: string; rxPower: number; txPower: number }>> {
    // In live network environment, this issues Net-SNMP get-table request or executes SSH script
    const profile = VENDOR_PROFILES[vendor] || VENDOR_PROFILES.VSOL;
    
    // Returns realistic scraped data variations
    return [
      { macSerial: 'VSOL-48A1092F', rxPower: -18.4 + (Math.random() * 0.4 - 0.2), txPower: 2.1 },
      { macSerial: 'VSOL-99B231C1', rxPower: -25.2 + (Math.random() * 0.4 - 0.2), txPower: 1.8 },
      { macSerial: 'HWTC-55A38411', rxPower: -29.8 + (Math.random() * 0.4 - 0.2), txPower: 0.2 },
      { macSerial: 'HWTC-78F9A120', rxPower: -19.5, txPower: 2.3 }
    ];
  }
}
