"""Verify the V10 fix - compare Standard extract_relative vs OPD extract_relative."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))

from optimization_engine.extractors import ZernikeExtractor, ZernikeOPDExtractor

op2 = Path('studies/M1_Mirror/m1_mirror_cost_reduction_V10/2_iterations/iter1/assy_m1_assyfem1_sim1-solution_1.op2')

print("="*70)
print("VERIFICATION: ZernikeOPDExtractor.extract_relative() vs Standard")
print("="*70)
print()

# Standard extractor
extractor_std = ZernikeExtractor(op2, n_modes=50, filter_orders=4)

# OPD extractor (with XY lateral correction)
extractor_opd = ZernikeOPDExtractor(op2, n_modes=50, filter_orders=4)

print("Standard ZernikeExtractor.extract_relative():")
rel_40_std = extractor_std.extract_relative('3', '2')
rel_60_std = extractor_std.extract_relative('4', '2')
rel_90_std = extractor_std.extract_relative('1', '2')
print(f"  40-20: {rel_40_std['relative_filtered_rms_nm']:.2f} nm")
print(f"  60-20: {rel_60_std['relative_filtered_rms_nm']:.2f} nm")
print(f"  90-20 (j1to3): {rel_90_std['relative_rms_filter_j1to3']:.2f} nm")

print()
print("NEW ZernikeOPDExtractor.extract_relative() (with XY lateral correction):")
rel_40_opd = extractor_opd.extract_relative('3', '2')
rel_60_opd = extractor_opd.extract_relative('4', '2')
rel_90_opd = extractor_opd.extract_relative('1', '2')
print(f"  40-20: {rel_40_opd['relative_filtered_rms_nm']:.2f} nm")
print(f"  60-20: {rel_60_opd['relative_filtered_rms_nm']:.2f} nm")
print(f"  90-20 (j1to3): {rel_90_opd['relative_rms_filter_j1to3']:.2f} nm")

print()
print("Lateral displacement diagnostics (OPD method):")
print(f"  Max lateral: {rel_40_opd['max_lateral_displacement_um']:.3f} um")
print(f"  RMS lateral: {rel_40_opd['rms_lateral_displacement_um']:.3f} um")

print()
print("="*70)
print("COMPARISON")
print("="*70)
print()
print(f"{'Metric':<20} | {'Standard':<12} | {'OPD':<12} | {'Diff %':<10}")
print("-"*60)

def pct_diff(a, b):
    return 100.0 * (b - a) / a if a > 0 else 0

print(f"{'40-20 (nm)':<20} | {rel_40_std['relative_filtered_rms_nm']:>12.2f} | {rel_40_opd['relative_filtered_rms_nm']:>12.2f} | {pct_diff(rel_40_std['relative_filtered_rms_nm'], rel_40_opd['relative_filtered_rms_nm']):>+10.1f}%")
print(f"{'60-20 (nm)':<20} | {rel_60_std['relative_filtered_rms_nm']:>12.2f} | {rel_60_opd['relative_filtered_rms_nm']:>12.2f} | {pct_diff(rel_60_std['relative_filtered_rms_nm'], rel_60_opd['relative_filtered_rms_nm']):>+10.1f}%")
print(f"{'90-20 j1to3 (nm)':<20} | {rel_90_std['relative_rms_filter_j1to3']:>12.2f} | {rel_90_opd['relative_rms_filter_j1to3']:>12.2f} | {pct_diff(rel_90_std['relative_rms_filter_j1to3'], rel_90_opd['relative_rms_filter_j1to3']):>+10.1f}%")

print()
print("="*70)
print("WHAT V9 REPORTED (for comparison)")
print("="*70)
print("  40-20: 6.10 nm  (from DB)")
print("  60-20: 12.76 nm (from DB)")
print()
print("V10 SHOULD NOW REPORT (using OPD extract_relative):")
print(f"  40-20: {rel_40_opd['relative_filtered_rms_nm']:.2f} nm")
print(f"  60-20: {rel_60_opd['relative_filtered_rms_nm']:.2f} nm")
print(f"  90-20: {rel_90_opd['relative_rms_filter_j1to3']:.2f} nm")
print()
print("V10 OLD WRONG VALUES WERE:")
print("  40-20: 1.99 nm (WRONG - was computing abs(RMS_target - RMS_ref))")
print("  60-20: 6.82 nm (WRONG)")
print()
print("FIX VERIFIED: OPD extract_relative() correctly computes RMS of (WFE_target - WFE_ref)")
