from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.management.base import BaseCommand

User = get_user_model()


class Command(BaseCommand):
    help = "Set up social authentication through admin interface"

    def handle(self, *args, **options):
        # Get or create the default site
        site, _ = Site.objects.get_or_create(
            id=1,
            defaults={
                "domain": "localhost:8000",
                "name": "ThrillWiki Development",
            },
        )
        if not _:
            site.domain = "localhost:8000"
            site.name = "ThrillWiki Development"
            site.save()
        self.stdout.write(f"{'Created' if _ else 'Updated'} site: {site.domain}")

        # Create superuser if it doesn't exist
        if not User.objects.filter(username="admin").exists():
            admin_user = User.objects.create(
                username="admin",
                email="admin@example.com",
                is_staff=True,
                is_superuser=True,
            )
            admin_user.set_password("admin")
            admin_user.save()
            self.stdout.write("Created superuser: admin/admin")

        self.stdout.write(
            self.style.SUCCESS(
                """
Social auth setup instructions:

1. Run the development server:
   uv run manage.py runserver_plus

2. Go to the admin interface:
   http://localhost:8000/admin/

3. Log in with:
   Username: admin
   Password: admin

4. Add social applications:
   - Go to "Social applications" under "Social Accounts"
   - Add Discord app:
     Provider: discord
     Name: Discord
     Client id: 1299112802274902047
     Secret key: ece7Pe_M4mD4mYzAgcINjTEKL_3ftL11
     Sites: Add "localhost:8000"

   - Add Google app:
     Provider: google
     Name: Google
     Client id: 135166769591-nopcgmo0fkqfqfs9qe783a137mtmcrt2.apps.googleusercontent.com
     Secret key: GOCSPX-Wd_0Ue0Ue0Ue0Ue0Ue0Ue0Ue0Ue
     Sites: Add "localhost:8000"
"""
            )
        )
