# Databricks notebook source
# COMMAND ----------
import subprocess, sys

LOG = "/Workspace/insurance-causal/debug_install_log2.txt"
lines = [f"Python: {sys.version}", ""]

# Check if econml is already available
result = subprocess.run([sys.executable, "-m", "pip", "show", "econml"], capture_output=True, text=True)
lines.append(f"econml pre-installed: {result.stdout[:200] if result.returncode == 0 else 'NO'}")

# Check what's already installed (key packages)
for pkg in ["econml", "numpy", "scipy", "catboost", "sklearn", "polars"]:
    result = subprocess.run([sys.executable, "-c", f"import {pkg}; print({pkg}.__version__)"], capture_output=True, text=True)
    if result.returncode == 0:
        lines.append(f"  {pkg}: {result.stdout.strip()}")
    else:
        lines.append(f"  {pkg}: NOT IMPORTABLE ({result.stderr.strip()[:100]})")

lines.append("")

# Try installing econml with a specific older version that has wheels
for ver in ["0.15.1", "0.15.0", "0.14.1"]:
    result = subprocess.run(
        [sys.executable, "-m", "pip", "install", f"econml=={ver}", "--no-cache-dir", "-q"],
        capture_output=True, text=True
    )
    status = "OK" if result.returncode == 0 else "FAIL"
    lines.append(f"econml=={ver}: {status}")
    if result.returncode == 0:
        break
    else:
        lines.append(f"  {result.stderr[-200:]}")

with open(LOG, "w") as f:
    f.write("\n".join(lines))

print("Done - see", LOG)
