"""Test bidirectional conversion between INI and JSON formats.

This script verifies that we can:
1. Load from INI → save to JSON
2. Load from JSON → save to INI
3. Compare the results
"""
from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).parent.parent))

from pydownloadfwviasrio.profiles import ProfileManager


def main() -> None:
    """Test INI ↔ JSON conversion."""
    
    # Step 1: Load from original INI
    print("Step 1: Loading from original targets.ini...")
    manager1 = ProfileManager()
    manager1.load_from_ini(Path("_OLD/Vecchia_app/FpgaBeamMeUp/targets.ini"))
    
    # Step 2: Save to JSON
    print("Step 2: Saving to flash_profiles.json...")
    manager1.config_path = Path("flash_profiles.json")
    manager1.save()
    
    # Step 3: Load from JSON
    print("Step 3: Loading from flash_profiles.json...")
    manager2 = ProfileManager(Path("flash_profiles.json"))
    
    # Step 4: Export back to INI
    print("Step 4: Exporting back to test_output.ini...")
    manager2.export_to_ini(Path("test_output.ini"))
    
    # Verify data consistency
    print("\n" + "=" * 60)
    print("VERIFICATION")
    print("=" * 60)
    
    print(f"\nGlobal Config Match: ", end="")
    if (manager1.global_config.ip == manager2.global_config.ip and
        manager1.global_config.port == manager2.global_config.port and
        manager1.global_config.default_target == manager2.global_config.default_target):
        print("✓ PASSED")
    else:
        print("✗ FAILED")
    
    print(f"Models Count Match: ", end="")
    if len(manager1.models) == len(manager2.models):
        print(f"✓ PASSED ({len(manager1.models)} models)")
    else:
        print(f"✗ FAILED ({len(manager1.models)} vs {len(manager2.models)})")
    
    print(f"Targets Count Match: ", end="")
    if len(manager1.targets) == len(manager2.targets):
        print(f"✓ PASSED ({len(manager1.targets)} targets)")
    else:
        print(f"✗ FAILED ({len(manager1.targets)} vs {len(manager2.targets)})")
    
    # Check individual models
    print(f"\nModel Details Match: ", end="")
    models_match = True
    for id_model in manager1.models:
        m1 = manager1.models[id_model]
        m2 = manager2.models.get(id_model)
        if not m2:
            models_match = False
            break
        if (m1.model != m2.model or 
            m1.num_sectors != m2.num_sectors or
            m1.golden_start != m2.golden_start):
            models_match = False
            break
    print("✓ PASSED" if models_match else "✗ FAILED")
    
    # Check individual targets
    print(f"Target Details Match: ", end="")
    targets_match = True
    for id_target in manager1.targets:
        t1 = manager1.targets[id_target]
        t2 = manager2.targets.get(id_target)
        if not t2:
            targets_match = False
            break
        if (t1.slot_address != t2.slot_address or
            t1.architecture != t2.architecture or
            t1.id_model != t2.id_model):
            targets_match = False
            break
    print("✓ PASSED" if targets_match else "✗ FAILED")
    
    print("\n" + "=" * 60)
    print("Files generated:")
    print(f"  - flash_profiles.json (JSON format)")
    print(f"  - test_output.ini (INI format)")
    print("=" * 60)


if __name__ == "__main__":
    main()
