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


class Command(BaseCommand):
    help = "Reset social apps configuration"

    def handle(self, *args, **options):
        # Delete all social apps using raw SQL to bypass Django's ORM
        with connection.cursor() as cursor:
            cursor.execute("DELETE FROM socialaccount_socialapp_sites")
            cursor.execute("DELETE FROM socialaccount_socialapp")

        # Get the default site
        site = Site.objects.get(id=1)

        # Create Discord app
        discord_app = SocialApp.objects.create(
            provider="discord",
            name="Discord",
            client_id="1299112802274902047",
            secret="ece7Pe_M4mD4mYzAgcINjTEKL_3ftL11",
        )
        discord_app.sites.add(site)
        self.stdout.write(f"Created Discord app with ID: {discord_app.pk}")

        # Create Google app
        google_app = SocialApp.objects.create(
            provider="google",
            name="Google",
            client_id=("135166769591-nopcgmo0fkqfqfs9qe783a137mtmcrt2.apps.googleusercontent.com"),
            secret="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",
        )
        google_app.sites.add(site)
        self.stdout.write(f"Created Google app with ID: {google_app.pk}")
