
from django.core.management.base import BaseCommand
from django.utils import timezone

from NexusApp.models import AdminUser


class Command(BaseCommand):
    help = 'Create or update the admin user'

    def add_arguments(self, parser):
        parser.add_argument('--email', default='admin@gmail.com')
        parser.add_argument('--password', default='admin',
                            help='Use a strong password on a public server.')

    def handle(self, *args, **kwargs):

        email = kwargs['email']
        password = kwargs['password']

        user, created = AdminUser.objects.get_or_create(
            email=email,
            defaults={
                'name': 'admin',
                'designation': 'Admin',
                'role': 'admin',
            }
        )

        user.name = 'admin'
        user.phone = None
        user.designation = 'Admin'
        user.email_verified_at = user.email_verified_at or timezone.now()
        user.role = 'admin'

        # Always store a Django-hashed password
        user.set_password(password)

        user.save()

        if created:
            self.stdout.write(
                self.style.SUCCESS('Admin created successfully!')
            )
        else:
            self.stdout.write(
                self.style.SUCCESS('Admin updated successfully!')
            )
