from django.core.management.base import BaseCommand

from servala.core.models import User


class Command(BaseCommand):
    help = "Turn a user into a superuser. Use with caution."

    def add_arguments(self, parser):
        parser.add_argument(
            "email",
            type=str,
            help="Email address of user to grant superuser permissions",
        )

    def handle(self, *args, **options):
        email = options["email"]

        user = User.objects.filter(email__iexact=email).first()
        if not user:
            self.stdout.write(self.style.ERROR("No matching user found."))
            return

        user.is_superuser = True
        user.is_staff = True
        user.save()
        self.stdout.write(self.style.SUCCESS(f"{user} is now a superuser."))
