"""
Management command to create or update admin user
"""
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model


class Command(BaseCommand):
    help = 'Create or update admin user with specified credentials'
    
    def add_arguments(self, parser):
        parser.add_argument('--username', type=str, default='admin', help='Admin username')
        parser.add_argument('--email', type=str, default='admin@branchbusinessadvance.com', help='Admin email')
        parser.add_argument('--password', type=str, default='admin123', help='Admin password')
        parser.add_argument('--phone', type=str, default='+254700000000', help='Admin phone number')
    
    def handle(self, *args, **options):
        User = get_user_model()
        
        username = options['username']
        email = options['email']
        password = options['password']
        phone = options['phone']
        
        try:
            # Try to find existing user by email first
            user = User.objects.get(email=email)
            self.stdout.write(f'Found existing user by email: {user.email}')
            user.username = username
            user.set_password(password)
            user.is_superuser = True
            user.is_staff = True
            user.is_active = True
            user.role = 'admin'
            if not user.phone_number:
                user.phone_number = phone
            user.save()
            self.stdout.write(
                self.style.SUCCESS(f'Updated existing admin user: {user.email}')
            )
            
        except User.DoesNotExist:
            try:
                # Try to find by username
                user = User.objects.get(username=username)
                self.stdout.write(f'Found existing user by username: {user.username}')
                user.email = email
                user.set_password(password)
                user.is_superuser = True
                user.is_staff = True
                user.is_active = True
                user.role = 'admin'
                if not user.phone_number:
                    user.phone_number = phone
                user.save()
                self.stdout.write(
                    self.style.SUCCESS(f'Updated existing admin user: {user.username}')
                )
                
            except User.DoesNotExist:
                # Create new user
                user = User.objects.create_superuser(
                    username=username,
                    email=email,
                    password=password,
                    first_name='Admin',
                    last_name='User',
                    phone_number=phone,
                    role='admin'
                )
                self.stdout.write(
                    self.style.SUCCESS(f'Created new superuser: {user.email}')
                )
        
        self.stdout.write('')
        self.stdout.write(self.style.SUCCESS('Admin user is ready!'))
        self.stdout.write(f'Username: {user.username}')
        self.stdout.write(f'Email: {user.email}')
        self.stdout.write(f'Password: {password}')
        self.stdout.write('')
        self.stdout.write('Login URLs:')
        self.stdout.write('• Admin Panel: http://localhost:8000/admin/')
        self.stdout.write('• Reports Dashboard: http://localhost:8000/reports/')
        self.stdout.write('• Main Dashboard: http://localhost:8000/dashboard/')