#!/usr/bin/env python
"""
Test all school system models
"""
import os
import sys
import django
from datetime import date, timedelta

# Setup Django
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
django.setup()

print("=" * 60)
print("🧪 TESTING ALL SCHOOL SYSTEM MODELS")
print("=" * 60)

try:
    # Test 1: Import all models
    from core.models import CustomUser, School
    from students.models import Student, ParentGuardian
    from academics.models import SchoolClass, Subject, Attendance
    from finance.models import FeeStructure, Invoice, Payment
    from communication.models import SMSMessage, Announcement
    
    print("✅ All models imported successfully!")
    
    # Test 2: Create test data
    print("\n📊 Creating test data...")
    
    # Create a school
    school, created = School.objects.get_or_create(
        code="DOU-PRIMARY",
        defaults={
            'name': 'Douala Primary School',
            'address': 'Bonapriso, Douala',
            'city': 'Douala',
            'quarter': 'Bonapriso',
            'phone': '+237 233 000 000',
            'principal_name': 'Mr. Emmanuel',
        }
    )
    
    # Create a student
    student, created = Student.objects.get_or_create(
        student_id="2025-001",
        defaults={
            'first_name': 'Test',
            'last_name': 'Student',
            'date_of_birth': date(2018, 5, 15),
            'gender': 'M',
            'home_address': 'Test Address',
            'quarter': 'Bonapriso',
            'emergency_contact_phone': '+237 6xx xxx xxx',
            'emergency_contact_name': 'Test Parent',
            'emergency_contact_relationship': 'Guardian',
            'admission_date': date.today(),
        }
    )
    
    # Create a class
    school_class, created = SchoolClass.objects.get_or_create(
        code="CP-A",
        defaults={
            'level': 'cp',
            'section': 'A',
        }
    )
    
    # Create attendance
    attendance, created = Attendance.objects.get_or_create(
        student=student,
        date=date.today(),
        defaults={
            'school_class': school_class,
            'status': 'present',
        }
    )
    
    # Create fee structure
    fee, created = FeeStructure.objects.get_or_create(
        name="Tuition Fee",
        academic_year="2023-2024",
        defaults={
            'fee_type': 'tuition',
            'amount': 50000,
            'payment_frequency': 'termly',
        }
    )
    
    print(f"✅ Created: {school}")
    print(f"✅ Created: {student}")
    print(f"✅ Created: {school_class}")
    print(f"✅ Created: Attendance for {attendance.date}")
    print(f"✅ Created: {fee}")
    
    # Test 3: Count records
    print("\n📈 Model Statistics:")
    print(f"  Schools: {School.objects.count()}")
    print(f"  Students: {Student.objects.count()}")
    print(f"  Classes: {SchoolClass.objects.count()}")
    print(f"  Attendance records: {Attendance.objects.count()}")
    print(f"  Fee structures: {FeeStructure.objects.count()}")
    
    # Test 4: Verify relationships work
    print("\n🔗 Testing relationships...")
    print(f"  Student age: {student.get_age()} years")
    print(f"  Class full name: {school_class}")
    print(f"  Fee amount: {fee.amount} XAF")
    
    print("\n" + "=" * 60)
    print("🎉 ALL MODEL TESTS PASSED!")
    print("=" * 60)
    print("\n✅ Your school system is ready!")
    print(f"\n🔗 Admin URL: http://127.0.0.1:8000/admin/")
    print(f"📱 API Base URL: http://127.0.0.1:8000/api/")
    
except Exception as e:
    print(f"\n❌ ERROR: {str(e)}")
    import traceback
    traceback.print_exc()