from __future__ import annotations

def execute(payload: dict) -> dict:
    lux = max(0.0, float(payload.get("brightness_lux", 500)))
    rain = min(1.0, max(0.0, float(payload.get("rain_level", 0))))
    dust = min(1.0, max(0.0, float(payload.get("dust_level", 0))))
    threshold = float(payload.get("base_threshold", 0.5))
    threshold += 0.08 if lux < 50 else (-0.03 if lux > 1000 else 0)
    threshold += 0.1 * rain + 0.08 * dust
    primary = "radar" if rain > 0.6 or dust > 0.6 else ("thermal_camera" if lux < 30 else "rgb_camera")
    return {"detection_threshold": round(min(0.95, threshold), 3), "primary_sensor": primary, "degradation": round(max(rain, dust, max(0, (50-lux)/50)), 3)}
