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


class Command(BaseCommand):
    help = "Update social apps to be associated with all sites"

    def handle(self, *args, **options):
        # Get all sites
        sites = Site.objects.all()

        # Update each social app
        for app in SocialApp.objects.all():
            self.stdout.write(f"Updating {app.provider} app...")
            # Clear existing sites
            app.sites.clear()
            # Add all sites
            for site in sites:
                app.sites.add(site)
            self.stdout.write(f"Added sites: {', '.join(site.domain for site in sites)}")
