from allauth.socialaccount.models import SocialApp
from django.conf import settings
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    help = "Verify Discord OAuth2 settings"

    def handle(self, *args, **options):
        # Get Discord app
        try:
            discord_app = SocialApp.objects.get(provider="discord")
            self.stdout.write("Found Discord app configuration:")
            self.stdout.write(f"Client ID: {discord_app.client_id}")
            self.stdout.write(f"Secret: {discord_app.secret}")

            # Get sites
            sites = discord_app.sites.all()
            self.stdout.write("\nAssociated sites:")
            for site in sites:
                self.stdout.write(f"- {site.domain} ({site.name})")

            # Show callback URL
            callback_url = "http://localhost:8000/accounts/discord/login/callback/"
            self.stdout.write("\nCallback URL to configure in Discord Developer Portal:")
            self.stdout.write(callback_url)

            # Show OAuth2 settings
            self.stdout.write("\nOAuth2 settings in settings.py:")
            discord_settings = settings.SOCIALACCOUNT_PROVIDERS.get("discord", {})
            self.stdout.write(f"PKCE Enabled: {discord_settings.get('OAUTH_PKCE_ENABLED', False)}")
            self.stdout.write(f"Scopes: {discord_settings.get('SCOPE', [])}")

        except SocialApp.DoesNotExist:
            self.stdout.write(self.style.ERROR("Discord app not found"))
