from allauth.socialaccount.models import SocialApp
from django.contrib.sites.models import Site
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    help = "Set up social authentication providers for development"

    def handle(self, *args, **options):
        # Get the current site
        site = Site.objects.get_current()
        self.stdout.write(f"Setting up social providers for site: {site}")

        # Clear existing social apps to avoid duplicates
        deleted_count = SocialApp.objects.all().delete()[0]
        self.stdout.write(f"Cleared {deleted_count} existing social apps")

        # Create Google social app
        google_app = SocialApp.objects.create(
            provider="google",
            name="Google",
            client_id="demo-google-client-id.apps.googleusercontent.com",
            secret="demo-google-client-secret",
            key="",
        )
        google_app.sites.add(site)
        self.stdout.write(self.style.SUCCESS("✅ Created Google social app"))

        # Create Discord social app
        discord_app = SocialApp.objects.create(
            provider="discord",
            name="Discord",
            client_id="demo-discord-client-id",
            secret="demo-discord-client-secret",
            key="",
        )
        discord_app.sites.add(site)
        self.stdout.write(self.style.SUCCESS("✅ Created Discord social app"))

        # List all social apps
        self.stdout.write("\nConfigured social apps:")
        for app in SocialApp.objects.all():
            self.stdout.write(f"- {app.name} ({app.provider}): {app.client_id}")

        self.stdout.write(self.style.SUCCESS(f"\nTotal social apps: {SocialApp.objects.count()}"))
