from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from chat.models import UserProfile

class Command(BaseCommand):
    help = 'Create default admin user if it does not exist'

    def handle(self, *args, **kwargs):
        if not User.objects.filter(username='admin').exists():
            u = User.objects.create_superuser('admin', 'admin@example.com', 'admin')
            p, _ = UserProfile.objects.get_or_create(user=u)
            p.role = 'admin'
            p.save()
            self.stdout.write(self.style.SUCCESS('Successfully created admin user.'))
        else:
            self.stdout.write(self.style.SUCCESS('Admin user already exists.'))
