#!/usr/bin/env python3
"""
D-Bus Communication Test for apt-ostree
This script tests the D-Bus interface programmatically
"""

import sys
import dbus
import dbus.exceptions

def test_dbus_connection():
    """Test basic D-Bus connection to apt-ostree daemon"""
    print("=== apt-ostree D-Bus Python Test ===")
    print()
    
    try:
        # Connect to system bus
        print("1. Connecting to system D-Bus...")
        bus = dbus.SystemBus()
        print("✓ Connected to system D-Bus")
        
        # Get the apt-ostree daemon object
        print("\n2. Getting apt-ostree daemon object...")
        daemon = bus.get_object('org.aptostree.dev', '/org/aptostree/dev')
        print("✓ Got daemon object")
        
        # Get the interface
        print("\n3. Getting daemon interface...")
        interface = dbus.Interface(daemon, 'org.aptostree.dev.Daemon')
        print("✓ Got daemon interface")
        
        # Test Ping method
        print("\n4. Testing Ping method...")
        result = interface.Ping()
        print(f"✓ Ping result: {result}")
        
        # Test GetStatus method
        print("\n5. Testing GetStatus method...")
        status = interface.GetStatus()
        print(f"✓ Status result: {status}")
        
        # List available methods
        print("\n6. Available methods:")
        methods = daemon.Introspect()
        print(methods)
        
        print("\n=== All tests passed! ===")
        return True
        
    except dbus.exceptions.DBusException as e:
        print(f"✗ D-Bus error: {e}")
        return False
    except Exception as e:
        print(f"✗ General error: {e}")
        return False

def test_dbus_introspection():
    """Test D-Bus introspection"""
    print("\n=== D-Bus Introspection Test ===")
    
    try:
        bus = dbus.SystemBus()
        daemon = bus.get_object('org.aptostree.dev', '/org/aptostree/dev')
        
        # Get introspection data
        introspection = daemon.Introspect()
        print("✓ Introspection successful")
        print("Introspection data:")
        print(introspection)
        
        return True
        
    except Exception as e:
        print(f"✗ Introspection failed: {e}")
        return False

def test_dbus_properties():
    """Test D-Bus properties"""
    print("\n=== D-Bus Properties Test ===")
    
    try:
        bus = dbus.SystemBus()
        daemon = bus.get_object('org.aptostree.dev', '/org/aptostree/dev')
        
        # Get properties interface
        props = dbus.Interface(daemon, 'org.freedesktop.DBus.Properties')
        
        # List all properties
        all_props = props.GetAll('org.aptostree.dev.Daemon')
        print("✓ Properties retrieved")
        print("Properties:")
        for key, value in all_props.items():
            print(f"  {key}: {value}")
        
        return True
        
    except Exception as e:
        print(f"✗ Properties test failed: {e}")
        return False

def main():
    """Main test function"""
    success = True
    
    # Test basic connection
    if not test_dbus_connection():
        success = False
    
    # Test introspection
    if not test_dbus_introspection():
        success = False
    
    # Test properties
    if not test_dbus_properties():
        success = False
    
    if success:
        print("\n🎉 All D-Bus tests passed!")
        sys.exit(0)
    else:
        print("\n❌ Some D-Bus tests failed!")
        sys.exit(1)

if __name__ == "__main__":
    main() 