import os

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


class Command(BaseCommand):
    help = "Fix social app configurations"

    def handle(self, *args, **options):
        # Delete all existing social apps
        SocialApp.objects.all().delete()
        self.stdout.write("Deleted all existing social apps")

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

        # Create Google provider
        google_app = SocialApp.objects.create(
            provider="google",
            name="Google",
            client_id=os.getenv("GOOGLE_CLIENT_ID"),
            secret=os.getenv("GOOGLE_CLIENT_SECRET"),
        )
        google_app.sites.add(site)
        self.stdout.write(f"Created Google app with client_id: {google_app.client_id}")

        # Create Discord provider
        discord_app = SocialApp.objects.create(
            provider="discord",
            name="Discord",
            client_id=os.getenv("DISCORD_CLIENT_ID"),
            secret=os.getenv("DISCORD_CLIENT_SECRET"),
        )
        discord_app.sites.add(site)
        self.stdout.write(f"Created Discord app with client_id: {discord_app.client_id}")
