from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.db import transaction

class Command(BaseCommand):
    help = 'Initializes basic authentication data: creates a superuser and a regular user.'

    def handle(self, *args, **options):
        User = get_user_model()

        self.stdout.write(self.style.SUCCESS('--- Initializing Authentication Data ---'))

        # Create Superuser
        superuser_email = 'admin@example.com'
        superuser_username = 'admin'
        superuser_password = 'adminpassword'

        with transaction.atomic():
            if not User.objects.filter(email=superuser_email).exists():
                self.stdout.write(f'Creating superuser: {superuser_email}...')
                User.objects.create_superuser(
                    email=superuser_email,
                    username=superuser_username,
                    password=superuser_password
                )
                self.stdout.write(self.style.SUCCESS('Superuser created successfully.'))
            else:
                self.stdout.write(self.style.WARNING(f'Superuser {superuser_email} already exists.'))

            # Create Regular User
            regular_user_email = 'user@example.com'
            regular_user_username = 'regularuser'
            regular_user_password = 'userpassword'

            if not User.objects.filter(email=regular_user_email).exists():
                self.stdout.write(f'Creating regular user: {regular_user_email}...')
                User.objects.create_user(
                    email=regular_user_email,
                    username=regular_user_username,
                    password=regular_user_password
                )
                self.stdout.write(self.style.SUCCESS('Regular user created successfully.'))
            else:
                self.stdout.write(self.style.WARNING(f'Regular user {regular_user_email} already exists.'))

        self.stdout.write(self.style.SUCCESS('--- Authentication Data Initialization Complete ---'))
