"""Test neural surrogate integration."""

import time
from optimization_engine.processors.surrogates.neural_surrogate import create_surrogate_for_study

print("Testing Neural Surrogate Integration")
print("=" * 60)

# Create surrogate with auto-detection
surrogate = create_surrogate_for_study()

if surrogate is None:
    print("ERROR: Failed to create surrogate")
    exit(1)

print(f"Surrogate created successfully!")
print(f"  Device: {surrogate.device}")
print(f"  Nodes: {surrogate.num_nodes}")
print(f"  Model val_loss: {surrogate.best_val_loss:.4f}")

# Test prediction
test_params = {
    "beam_half_core_thickness": 7.0,
    "beam_face_thickness": 3.0,
    "holes_diameter": 40.0,
    "hole_count": 10.0
}

print(f"\nTest prediction with params: {test_params}")
results = surrogate.predict(test_params)

print(f"\nResults:")
print(f"  Max displacement: {results['max_displacement']:.6f} mm")
print(f"  Max stress: {results['max_stress']:.2f} (approx)")
print(f"  Inference time: {results['inference_time_ms']:.2f} ms")

# Speed test
n = 100
start = time.time()
for _ in range(n):
    surrogate.predict(test_params)
elapsed = time.time() - start

print(f"\nSpeed test: {n} predictions in {elapsed:.3f}s")
print(f"  Average: {elapsed/n*1000:.2f} ms per prediction")

# Compare with FEA expectation
# From training data, typical max_displacement is ~0.02-0.03 mm
print(f"\nExpected range (from training data):")
print(f"  Max displacement: ~0.02-0.03 mm")
print(f"  Max stress: ~200-300 MPa")

stats = surrogate.get_statistics()
print(f"\nStatistics:")
print(f"  Total predictions: {stats['total_predictions']}")
print(f"  Average time: {stats['average_time_ms']:.2f} ms")

print("\nNeural surrogate test PASSED!")
