#!/usr/bin/env python3
"""Test script for BLE manager."""
import asyncio
import sys
from pathlib import Path

# Add backend to path
sys.path.insert(0, str(Path(__file__).parent / "app"))

from backend.managers.ble_manager import get_ble_manager, NotificationType


async def test_ble_manager():
    """Test BLE manager functionality."""
    print("Testing BLE Manager...")
    print("-" * 50)

    ble_manager = get_ble_manager()

    # Test initialization
    print("\n1. Testing initialization...")
    success = await ble_manager.initialize()
    if success:
        print("✅ BLE initialized successfully")
    else:
        print("⚠️  BLE not available (expected on non-Pi hardware)")

    # Test getting status
    print("\n2. Testing status retrieval...")
    status = await ble_manager.get_status()
    print(f"   Enabled: {status['enabled']}")
    print(f"   Available: {status['available']}")
    print(f"   Advertising: {status['advertising']}")
    print(f"   Connected devices: {status['connected_devices']}")
    print(f"   Device name: {status['device_name']}")
    print(f"   Queued notifications: {status['queued_notifications']}")

    # Test advertising (if available)
    if ble_manager.is_available():
        print("\n3. Testing advertising...")
        await ble_manager.start_advertising()
        print("✅ Advertising started")

        # Wait a moment
        await asyncio.sleep(1)

        await ble_manager.stop_advertising()
        print("✅ Advertising stopped")
    else:
        print("\n3. Skipping advertising test (BLE not available)")

    # Test sending notifications
    print("\n4. Testing notifications...")
    test_notifications = [
        (NotificationType.UPDATE_AVAILABLE, "Update v1.2.0 available"),
        (NotificationType.BATTERY_WARNING, "Battery at 20%"),
        (NotificationType.BATTERY_CRITICAL, "Battery critical: 10%"),
    ]

    for notif_type, message in test_notifications:
        await ble_manager.send_notification(
            notif_type,
            message,
            {"test": True}
        )
        print(f"   Sent: {notif_type.value}")

    # Check queued notifications
    status = await ble_manager.get_status()
    print(f"   Queued notifications: {status['queued_notifications']}")

    print("\n✅ BLE manager test complete!")


if __name__ == "__main__":
    asyncio.run(test_ble_manager())
