import os

from django.core.management.base import BaseCommand

from apps.lyric.models import User


class Command(BaseCommand):
    help = "Create a superuser if none exists"

    def handle(self, *args, **options):
        if User.objects.filter(is_superuser=True).exists():
            self.stdout.write("Superuser already exists!")
            return
        email = os.environ.get('DJANGO_SUPERUSER_EMAIL')
        password = os.environ.get('DJANGO_SUPERUSER_PASSWORD')
        if not email or not password:
            self.stdout.write("Superuser credentials not set!—skipping")
            return
        User.objects.create_superuser(email=email, password=password)
        self.stdout.write("Superuser created!")