"""Example usage of the new profile management system.

This script demonstrates how to:
1. Load profiles from JSON
2. Access global configuration
3. Retrieve targets with their models
4. Iterate through available targets
"""
from pathlib import Path
import sys

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))

from pydownloadfwviasrio.profiles import ProfileManager


def main() -> None:
    """Demonstrate profile management usage."""
    
    # Load existing configuration
    manager = ProfileManager(Path("flash_profiles.json"))
    
    print("=" * 60)
    print("GLOBAL CONFIGURATION")
    print("=" * 60)
    print(f"IP Address: {manager.global_config.ip}")
    print(f"Port: {manager.global_config.port}")
    print(f"SRIO Base: 0x{manager.global_config.srio_base:X}")
    print(f"FPGA Base: 0x{manager.global_config.fpga_base:X}")
    print(f"FPGA Sector Size: 0x{manager.global_config.fpga_sector:X}")
    print(f"Default Target: {manager.global_config.default_target}")
    
    print("\n" + "=" * 60)
    print("AVAILABLE MODELS")
    print("=" * 60)
    for id_model, model in sorted(manager.models.items()):
        print(f"\nModel ID {id_model}: {model.model}")
        print(f"  Description: {model.description}")
        print(f"  Flash Type: {model.flash_type}")
        print(f"  Addressing: {'4-byte' if model.is_4byte_addressing else '3-byte'}")
        print(f"  Sectors: {model.num_sectors}")
        print(f"  Golden Area: 0x{model.golden_start:08X} - 0x{model.golden_stop:08X}")
        print(f"  User Area:   0x{model.user_start:08X} - 0x{model.user_stop:08X}")
        if model.test_address:
            print(f"  Test Address: 0x{model.test_address:08X}")
    
    print("\n" + "=" * 60)
    print("AVAILABLE TARGETS")
    print("=" * 60)
    for id_target, target in sorted(manager.targets.items()):
        print(f"\nTarget: {id_target}")
        print(f"  Description: {target.description}")
        print(f"  Slot Address: 0x{target.slot_address:02X}")
        print(f"  Architecture: {target.architecture}")
        print(f"  File Prefix: {target.file_prefix}")
        
        # Get associated model
        model = manager.get_model(target.id_model)
        if model:
            print(f"  Model: {model.model} ({model.description})")
            print(f"  Memory Size: {(model.user_stop - model.golden_start + 1) / (1024*1024):.0f} MB")
    
    print("\n" + "=" * 60)
    print("EXAMPLE: Get default target with model")
    print("=" * 60)
    default_id = manager.global_config.default_target
    result = manager.get_target_with_model(default_id)
    
    if result:
        target, model = result
        print(f"\nDefault target: {target.id_target}")
        print(f"  Slot: 0x{target.slot_address:02X}")
        print(f"  IP: {manager.global_config.ip}:{manager.global_config.port}")
        print(f"  Model: {model.model}")
        print(f"  Sectors: {model.num_sectors}")
        print(f"  Addressing: {'4-byte' if model.is_4byte_addressing else '3-byte'}")
        print(f"  Golden: 0x{model.golden_start:08X} - 0x{model.golden_stop:08X}")
        print(f"  User:   0x{model.user_start:08X} - 0x{model.user_stop:08X}")
        
        # Calculate sizes
        golden_size = (model.golden_stop - model.golden_start + 1) / (1024 * 1024)
        user_size = (model.user_stop - model.user_start + 1) / (1024 * 1024)
        print(f"  Golden Size: {golden_size:.2f} MB")
        print(f"  User Size: {user_size:.2f} MB")


if __name__ == "__main__":
    main()
