-- Migration 110: Wavelength (TX/RX getrennt für BiDi), Connector-Normalisierung,
-- Data-Completeness-Score, Enrichment-Flag

-- 1. Wellenlängen-Felder aufteilen (BiDi hat TX ≠ RX)
ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS wavelength_tx_nm  INTEGER,   -- TX-Wellenlänge in nm (z.B. 1270)
  ADD COLUMN IF NOT EXISTS wavelength_rx_nm  INTEGER,   -- RX-Wellenlänge in nm (z.B. 1330)
  ADD COLUMN IF NOT EXISTS connector_type    TEXT,      -- 'LC', 'SC', 'MPO-12', 'MPO-16', 'RJ45', 'CS', 'SN'
  ADD COLUMN IF NOT EXISTS data_completeness INTEGER DEFAULT 0 CHECK (data_completeness BETWEEN 0 AND 100),
  ADD COLUMN IF NOT EXISTS enrichment_needed BOOLEAN DEFAULT FALSE,
  ADD COLUMN IF NOT EXISTS enrichment_fields TEXT[]  DEFAULT '{}';  -- welche Felder fehlen noch

-- 2. Indices für Performance
CREATE INDEX IF NOT EXISTS idx_tx_wavelength_tx ON transceivers (wavelength_tx_nm) WHERE wavelength_tx_nm IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_tx_wavelength_rx ON transceivers (wavelength_rx_nm) WHERE wavelength_rx_nm IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_tx_completeness  ON transceivers (data_completeness);
CREATE INDEX IF NOT EXISTS idx_tx_enrichment    ON transceivers (enrichment_needed) WHERE enrichment_needed = TRUE;

-- 3. Completeness-Berechnungsfunktion
CREATE OR REPLACE FUNCTION calc_data_completeness(
  p_form_factor TEXT, p_speed_gbps NUMERIC, p_fiber_type TEXT,
  p_reach_meters INTEGER, p_wavelength_tx INTEGER, p_connector TEXT
) RETURNS INTEGER AS $$
DECLARE
  score INTEGER := 0;
BEGIN
  IF p_form_factor IS NOT NULL AND p_form_factor != '' THEN score := score + 20; END IF;
  IF p_speed_gbps  IS NOT NULL AND p_speed_gbps > 0    THEN score := score + 20; END IF;
  IF p_fiber_type  IS NOT NULL AND p_fiber_type != ''  THEN score := score + 20; END IF;
  IF p_reach_meters IS NOT NULL AND p_reach_meters > 0  THEN score := score + 20; END IF;
  IF p_wavelength_tx IS NOT NULL AND p_wavelength_tx > 0 THEN score := score + 10; END IF;
  IF p_connector   IS NOT NULL AND p_connector != ''   THEN score := score + 10; END IF;
  RETURN score;
END;
$$ LANGUAGE plpgsql IMMUTABLE;

-- 4. Alle bestehenden Transceivers: Completeness initial berechnen
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
  );

-- 5. Trigger: Completeness automatisch aktualisieren
CREATE OR REPLACE FUNCTION trg_update_completeness()
RETURNS TRIGGER AS $$
BEGIN
  NEW.data_completeness := calc_data_completeness(
    NEW.form_factor, NEW.speed_gbps, NEW.fiber_type,
    NEW.reach_meters, NEW.wavelength_tx_nm, NEW.connector_type
  );
  NEW.enrichment_needed := (
    NEW.form_factor IS NULL OR NEW.speed_gbps IS NULL OR
    NEW.fiber_type IS NULL OR NEW.reach_meters IS NULL OR
    NEW.wavelength_tx_nm IS NULL OR NEW.connector_type IS NULL
  );
  -- Fehlende Felder dokumentieren
  NEW.enrichment_fields := ARRAY_REMOVE(ARRAY[
    CASE WHEN NEW.form_factor     IS NULL THEN 'form_factor'     END,
    CASE WHEN NEW.speed_gbps      IS NULL THEN 'speed_gbps'      END,
    CASE WHEN NEW.fiber_type      IS NULL THEN 'fiber_type'      END,
    CASE WHEN NEW.reach_meters    IS NULL THEN 'reach_meters'    END,
    CASE WHEN NEW.wavelength_tx_nm IS NULL THEN 'wavelength_tx_nm' END,
    CASE WHEN NEW.connector_type  IS NULL THEN 'connector_type'  END
  ], NULL);
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS trg_completeness ON transceivers;
CREATE TRIGGER trg_completeness
  BEFORE INSERT OR UPDATE ON transceivers
  FOR EACH ROW EXECUTE FUNCTION trg_update_completeness();

COMMENT ON COLUMN transceivers.wavelength_tx_nm  IS 'TX wavelength in nm. For BiDi: TX side. For duplex: both TX=RX.';
COMMENT ON COLUMN transceivers.wavelength_rx_nm  IS 'RX wavelength in nm. Only set for BiDi. NULL = same as TX.';
COMMENT ON COLUMN transceivers.connector_type    IS 'Physical connector: LC, SC, MPO-12, MPO-16, RJ45, CS, SN';
COMMENT ON COLUMN transceivers.data_completeness IS '0-100: percentage of mandatory fields filled (6 fields × weight)';
COMMENT ON COLUMN transceivers.enrichment_needed IS 'TRUE = one or more mandatory fields missing, enrichment job needed';
COMMENT ON COLUMN transceivers.enrichment_fields IS 'Array of field names that still need enrichment';
