#!/usr/bin/env python3
"""Test script for UPS 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.ups_manager import get_ups_manager


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

    ups_manager = get_ups_manager()

    # Test initialization
    print("\n1. Testing initialization...")
    success = await ups_manager.initialize()
    if success:
        print("✅ UPS initialized successfully")
    else:
        print(f"⚠️  UPS not available: {ups_manager._status.error_message}")
        print("   (This is expected on non-Pi hardware or without UPS HAT)")

    # Test getting status
    print("\n2. Testing status retrieval...")
    status = await ups_manager.get_status()
    print(f"   Battery: {status.battery_percentage:.1f}%")
    print(f"   Voltage: {status.voltage:.2f} mV")
    print(f"   Current: {status.current:.2f} mA")
    print(f"   Power Source: {status.power_source.value}")
    print(f"   Battery Status: {status.battery_status.value}")
    print(f"   Available: {status.is_available}")
    if status.error_message:
        print(f"   Error: {status.error_message}")

    # Test threshold setting
    print("\n3. Testing threshold configuration...")
    ups_manager.set_shutdown_threshold(15.0)
    ups_manager.set_warning_threshold(25.0)
    print("✅ Thresholds set: shutdown=15%, warning=25%")

    # Test event callback
    print("\n4. Testing event callbacks...")
    events_received = []

    async def test_callback(event):
        events_received.append(event)
        print(f"   Event received: {event['type']}")

    ups_manager.register_event_callback(test_callback)
    print("✅ Event callback registered")

    # Cleanup
    ups_manager.close()
    print("\n✅ UPS manager test complete!")


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