from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from core.models import Teacher, Student, Classroom, City, Governorate

class Command(BaseCommand):
    help = 'Creates test users: one teacher and one student'

    def handle(self, *args, **options):
        # 1. Setup Dependencies
        gov, _ = Governorate.objects.get_or_create(
            name_en="Muscat",
            defaults={'name_ar': "مسقط"}
        )
        city, _ = City.objects.get_or_create(
            name_en="Seeb", 
            defaults={'name_ar': "السيب", 'governorate': gov}
        )
        classroom, _ = Classroom.objects.get_or_create(
            name_en="Grade 10",
            defaults={'name_ar': "الصف 10", 'description': "Test Classroom"}
        )

        # 2. Create Teacher
        t_user, created = User.objects.get_or_create(username='teacher_test')
        if created:
            t_user.set_password('password123')
            t_user.email = 'teacher@example.com'
            t_user.first_name = 'John'
            t_user.last_name = 'Doe (Teacher)'
            t_user.save()
            
            Teacher.objects.create(
                user=t_user,
                bio="I am a test teacher specializing in Mathematics.",
                specialization="Mathematics"
            )
            self.stdout.write(self.style.SUCCESS(f"Created teacher: {t_user.username} (password123)"))
        else:
            self.stdout.write(self.style.WARNING(f"User {t_user.username} already exists"))

        # 3. Create Student
        s_user, created = User.objects.get_or_create(username='student_test')
        if created:
            s_user.set_password('password123')
            s_user.email = 'student@example.com'
            s_user.first_name = 'Jane'
            s_user.last_name = 'Smith (Student)'
            s_user.save()
            
            Student.objects.create(
                user=s_user,
                classroom=classroom,
                city=city,
                governorate=gov,
                mobile_number="1234567890",
                is_email_verified=True
            )
            self.stdout.write(self.style.SUCCESS(f"Created student: {s_user.username} (password123)"))
        else:
            self.stdout.write(self.style.WARNING(f"User {s_user.username} already exists"))