#!/usr/bin/env python
"""
Test script to verify the staff hierarchy API fix.
"""
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PX360.settings')
django.setup()

from apps.organizations.models import Staff

def test_api_data():
    """Test that staff data is available"""
    print("Testing Staff Hierarchy API Data...")
    print("-" * 60)
    
    # Get all staff
    staff_queryset = Staff.objects.all()
    total_staff = staff_queryset.count()
    print(f"✓ Total staff in database: {total_staff}")
    
    if total_staff == 0:
        print("\n⚠️  No staff data found!")
        return False
    
    # Get staff with managers
    staff_list = staff_queryset.select_related('report_to', 'hospital', 'department')
    
    # Find root nodes
    staff_dict = {staff.id: staff for staff in staff_list}
    root_staff = [
        staff for staff in staff_list 
        if staff.report_to_id is None or staff.report_to_id not in staff_dict
    ]
    
    print(f"✓ Root nodes (top-level managers): {len(root_staff)}")
    print()
    
    # Show sample data
    print("Sample Top-Level Managers:")
    for i, staff in enumerate(root_staff[:3], 1):
        print(f"  {i}. {staff.get_full_name()} ({staff.job_title or 'No title'})")
        print(f"     Hospital: {staff.hospital.name if staff.hospital else 'None'}")
        print(f"     Department: {staff.department.name if staff.department else 'None'}")
    
    if len(root_staff) > 3:
        print(f"  ... and {len(root_staff) - 3} more")
    
    print()
    print("-" * 60)
    print("✓ API data test PASSED!")
    print()
    print("The hierarchy API should now work correctly.")
    print("Visit http://localhost:8000/organizations/staff/hierarchy/ to view it.")
    return True

if __name__ == '__main__':
    success = test_api_data()
    exit(0 if success else 1)
