from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from core.models import Brand, Incident
import random

class Command(BaseCommand):
    help = 'Seeds the database with initial data for brands, incidents, and a default user.'

    def handle(self, *args, **options):
        self.stdout.write('Deleting existing data...')
        Incident.objects.all().delete()
        Brand.objects.all().delete()
        User.objects.filter(is_superuser=False).delete()

        self.stdout.write('Creating new data...')

        # Create a superuser if it doesn't exist
        if not User.objects.filter(username='admin').exists():
            User.objects.create_superuser('admin', 'admin@example.com', 'admin123')
            self.stdout.write(self.style.SUCCESS('Successfully created a superuser.'))

        # Get the admin user
        admin_user = User.objects.get(username='admin')

        # Create a brand and associate it with the admin user
        brand = Brand.objects.create(
            name="GuardiaNet",
            website="https://guardianet.example.com",
            logo_url="https://placehold.co/100x100/0A192F/FFFFFF/png?text=G",
        )
        brand.users.add(admin_user)

        # Sample data for incidents
        incidents_data = [
            {
                "incident_type": "phishing_attack",
                "source_url": "https://guardianet-login.scam.com/signin",
                "content": "A fraudulent website is impersonating our login page to steal user credentials. The design is a near-perfect replica of our own.",
                "status": "new",
            },
            {
                "incident_type": "negative_review",
                "source_url": "https://twitter.com/unhappy_user/status/1234567890",
                "content": "A highly negative and misleading tweet about our service uptime is gaining traction. It contains false information about a recent outage.",
                "status": "in_review",
            },
            {
                "incident_type": "domain_squatting",
                "source_url": "http://guardianet.org",
                "content": "A third party has registered the .org domain of our brand name and has put up a page with competitor ads.",
                "status": "new",
            },
            {
                "incident_type": "social_impersonation",
                "source_url": "https://instagram.com/guardianet_support_scam",
                "content": "An unofficial Instagram account is pretending to be our official support channel and asking users for their private information.",
                "status": "resolved",
            },
        ]

        for data in incidents_data:
            Incident.objects.create(
                brand=brand,
                incident_type=data["incident_type"],
                source_url=data["source_url"],
                content=data["content"],
                status=data["status"],
            )

        self.stdout.write(self.style.SUCCESS('Successfully seeded the database.'))
