"""
Management command to update admin user credentials
"""
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.db import transaction

User = get_user_model()


class Command(BaseCommand):
    help = 'Update admin user username and password'

    def add_arguments(self, parser):
        parser.add_argument(
            '--username',
            type=str,
            default='gnx',
            help='Admin username (default: gnx)'
        )
        parser.add_argument(
            '--password',
            type=str,
            default='P4eli240453',
            help='Admin password (default: P4eli240453)'
        )

    def handle(self, *args, **options):
        username = options['username']
        password = options['password']

        try:
            with transaction.atomic():
                # Try to find existing user with this username
                user = User.objects.filter(username=username).first()
                
                if user:
                    # Update existing user
                    user.set_password(password)
                    user.is_staff = True
                    user.is_superuser = True
                    user.is_active = True
                    user.save()
                    self.stdout.write(
                        self.style.SUCCESS(
                            f'✓ Successfully updated admin user "{username}"'
                        )
                    )
                else:
                    # Check if there are any existing superusers
                    existing_superusers = User.objects.filter(is_superuser=True)
                    
                    if existing_superusers.exists():
                        # Update the first superuser
                        user = existing_superusers.first()
                        old_username = user.username
                        user.username = username
                        user.set_password(password)
                        user.is_staff = True
                        user.is_superuser = True
                        user.is_active = True
                        user.save()
                        self.stdout.write(
                            self.style.SUCCESS(
                                f'✓ Successfully updated admin user from "{old_username}" to "{username}"'
                            )
                        )
                    else:
                        # Create new superuser
                        user = User.objects.create_user(
                            username=username,
                            password=password,
                            is_staff=True,
                            is_superuser=True,
                            is_active=True
                        )
                        self.stdout.write(
                            self.style.SUCCESS(
                                f'✓ Successfully created admin user "{username}"'
                            )
                        )
                
                self.stdout.write(
                    self.style.SUCCESS(
                        f'\nAdmin credentials:\n'
                        f'  Username: {username}\n'
                        f'  Password: {password}\n'
                        f'  Is Staff: {user.is_staff}\n'
                        f'  Is Superuser: {user.is_superuser}\n'
                        f'  Is Active: {user.is_active}\n'
                    )
                )
                
        except Exception as e:
            self.stdout.write(
                self.style.ERROR(f'❌ Error updating admin user: {str(e)}')
            )
            raise

