-- Migration 112: wavelengths TEXT → wavelength_tx_nm / wavelength_rx_nm INTEGER

UPDATE transceivers SET
  wavelength_tx_nm = CASE
    WHEN wavelengths ~ '^\s*850'          THEN 850
    WHEN wavelengths ~ '^\s*1270'         THEN 1270
    WHEN wavelengths ~ '^\s*1310'         THEN 1310
    WHEN wavelengths ~ '^\s*1330'         THEN 1330
    WHEN wavelengths ~ '^\s*1490'         THEN 1490
    WHEN wavelengths ~ '^\s*1550'         THEN 1550
    WHEN wavelengths ~ '^\s*1270\s*/\s*1330' THEN 1270
    WHEN wavelengths ~ '^\s*1330\s*/\s*1270' THEN 1330
    ELSE NULL
  END,
  wavelength_rx_nm = CASE
    WHEN wavelengths ~ '1270\s*/\s*1330' THEN 1330
    WHEN wavelengths ~ '1330\s*/\s*1270' THEN 1270
    WHEN wavelengths ~ '1310\s*/\s*1550' THEN 1550
    WHEN wavelengths ~ '1550\s*/\s*1310' THEN 1310
    ELSE NULL
  END
WHERE wavelengths IS NOT NULL
  AND wavelength_tx_nm IS NULL;

-- Connector aus alter connector-Spalte übernehmen (falls vorhanden)
UPDATE transceivers SET
  connector_type = CASE connector
    WHEN 'LC'     THEN 'LC'
    WHEN 'SC'     THEN 'SC'
    WHEN 'MPO'    THEN 'MPO-12'
    WHEN 'MPO-12' THEN 'MPO-12'
    WHEN 'MPO-16' THEN 'MPO-16'
    WHEN 'RJ45'   THEN 'RJ45'
    ELSE connector
  END
WHERE connector IS NOT NULL AND connector_type IS NULL;

-- Completeness neu berechnen nach Migration
UPDATE transceivers SET
  data_completeness = calc_data_completeness(
    form_factor, speed_gbps, fiber_type,
    reach_meters, wavelength_tx_nm, connector_type
  ),
  enrichment_needed = (
    form_factor IS NULL OR speed_gbps IS NULL OR
    fiber_type IS NULL OR reach_meters IS NULL OR
    wavelength_tx_nm IS NULL OR connector_type IS NULL
  );
