from django.core.management.base import BaseCommand
from django.db import transaction

# Import models from both apps
from apps.parks.models import Company as ParkCompany
from apps.rides.models.company import Company as RideCompany


class Command(BaseCommand):
    help = "Creates comprehensive sample data for the ThrillWiki theme park application"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.created_companies = {}
        self.created_parks = {}
        self.created_rides = {}

    def handle(self, *args, **options):
        self.stdout.write("Starting sample data creation...")

        try:
            with transaction.atomic():
                self.create_companies()
                self.create_parks()
                self.create_ride_models()
                self.create_rides()
                self.create_park_areas()
                self.create_reviews()

            self.stdout.write(self.style.SUCCESS("Successfully created comprehensive sample data!"))
            self.print_summary()

        except Exception as e:
            self.stdout.write(self.style.ERROR(f"Error creating sample data: {e}"))
            raise

    def create_companies(self):
        """Create companies with different roles following entity relationship rules"""
        self.stdout.write("Creating companies...")

        # Park operators and property owners (using parks.models.Company)
        park_operators_data = [
            {
                "name": "The Walt Disney Company",
                "slug": "walt-disney-company",
                "roles": ["OPERATOR", "PROPERTY_OWNER"],
                "description": "World's largest entertainment company and theme park operator.",
                "website": "https://www.disney.com/",
                "founded_year": 1923,
            },
            {
                "name": "Universal Parks & Resorts",
                "slug": "universal-parks-resorts",
                "roles": ["OPERATOR", "PROPERTY_OWNER"],
                "description": "Division of Comcast NBCUniversal, operating major theme parks worldwide.",
                "website": "https://www.universalparks.com/",
                "founded_year": 1964,
            },
            {
                "name": "Six Flags Entertainment Corporation",
                "slug": "six-flags-entertainment",
                "roles": ["OPERATOR", "PROPERTY_OWNER"],
                "description": "World's largest regional theme park company.",
                "website": "https://www.sixflags.com/",
                "founded_year": 1961,
            },
            {
                "name": "Cedar Fair Entertainment Company",
                "slug": "cedar-fair-entertainment",
                "roles": ["OPERATOR", "PROPERTY_OWNER"],
                "description": "One of North America's largest operators of regional amusement parks.",
                "website": "https://www.cedarfair.com/",
                "founded_year": 1983,
            },
            {
                "name": "Herschend Family Entertainment",
                "slug": "herschend-family-entertainment",
                "roles": ["OPERATOR", "PROPERTY_OWNER"],
                "description": "Largest family-owned themed attractions corporation in the United States.",
                "website": "https://www.hfecorp.com/",
                "founded_year": 1950,
            },
            {
                "name": "SeaWorld Parks & Entertainment",
                "slug": "seaworld-parks-entertainment",
                "roles": ["OPERATOR", "PROPERTY_OWNER"],
                "description": "Theme park and entertainment company focusing on nature-based themes.",
                "website": "https://www.seaworldentertainment.com/",
                "founded_year": 1959,
            },
            {
                "name": "Merlin Entertainments",
                "slug": "merlin-entertainments",
                "roles": ["OPERATOR", "PROPERTY_OWNER"],
                "description": "European theme park operator with LEGOLAND and Madame Tussauds brands.",
                "website": "https://www.merlinentertainments.com/",
                "founded_year": 1998,
            },
        ]

        for company_data in park_operators_data:
            company, created = ParkCompany.objects.get_or_create(slug=company_data["slug"], defaults=company_data)
            self.created_companies[company.slug] = company
            self.stdout.write(f"  {'Created' if created else 'Found'} park company: {company.name}")

        # Ride manufacturers and designers (using rides.models.Company)
        ride_companies_data = [
            {
                "name": "Bolliger & Mabillard",
                "slug": "bolliger-mabillard",
                "roles": ["MANUFACTURER", "DESIGNER"],
                "description": "Swiss roller coaster manufacturer known for inverted and diving coasters.",
                "website": "https://www.bolliger-mabillard.com/",
                "founded_date": "1988-01-01",
            },
            {
                "name": "Intamin Amusement Rides",
                "slug": "intamin-amusement-rides",
                "roles": ["MANUFACTURER", "DESIGNER"],
                "description": "Liechtenstein-based manufacturer of roller coasters and thrill rides.",
                "website": "https://www.intamin.com/",
                "founded_date": "1967-01-01",
            },
            {
                "name": "Arrow Dynamics",
                "slug": "arrow-dynamics",
                "roles": ["MANUFACTURER", "DESIGNER"],
                "description": "American manufacturer known for corkscrew coasters and mine trains.",
                "website": "https://en.wikipedia.org/wiki/Arrow_Dynamics",
                "founded_date": "1946-01-01",
            },
            {
                "name": "Vekoma Rides Manufacturing",
                "slug": "vekoma-rides-manufacturing",
                "roles": ["MANUFACTURER", "DESIGNER"],
                "description": "Dutch manufacturer of roller coasters and family rides.",
                "website": "https://www.vekoma.com/",
                "founded_date": "1926-01-01",
            },
            {
                "name": "Rocky Mountain Construction",
                "slug": "rocky-mountain-construction",
                "roles": ["MANUFACTURER", "DESIGNER"],
                "description": "American manufacturer specializing in I-Box track and Raptor track coasters.",
                "website": "https://www.rockymtnconstruction.com/",
                "founded_date": "2001-01-01",
            },
            {
                "name": "Mack Rides",
                "slug": "mack-rides",
                "roles": ["MANUFACTURER", "DESIGNER"],
                "description": "German manufacturer known for water rides and powered coasters.",
                "website": "https://www.mack-rides.com/",
                "founded_date": "1780-01-01",
            },
            {
                "name": "Chance Rides",
                "slug": "chance-rides",
                "roles": ["MANUFACTURER"],
                "description": "American manufacturer of thrill rides and amusement park equipment.",
                "website": "https://www.chancerides.com/",
                "founded_date": "1961-01-01",
            },
            {
                "name": "S&S Worldwide",
                "slug": "s-s-worldwide",
                "roles": ["MANUFACTURER", "DESIGNER"],
                "description": "American manufacturer known for drop towers and 4D free-fly coasters.",
                "website": "https://www.s-s.com/",
                "founded_date": "1990-01-01",
            },
            {
                "name": "Zierer Rides",
                "slug": "zierer-rides",
                "roles": ["MANUFACTURER"],
                "description": "German manufacturer of kiddie rides and family coasters.",
                "website": "https://www.zierer.com/",
                "founded_date": "1950-01-01",
            },
            {
                "name": "Gerstlauer",
                "slug": "gerstlauer",
                "roles": ["MANUFACTURER", "DESIGNER"],
                "description": "German manufacturer known for Euro-Fighter and spinning coasters.",
                "website": "https://www.gerstlauer-rides.de/",
                "founded_date": "1982-01-01",
            },
        ]

        for company_data in ride_companies_data:
            company, created = RideCompany.objects.get_or_create(slug=company_data["slug"], defaults=company_data)
            self.created_companies[company.slug] = company
            self.stdout.write(f"  {'Created' if created else 'Found'} ride company: {company.name}")

    def create_parks(self):
        """Create parks with proper operator relationships."""
        self.stdout.write("Creating parks...")

        # Park creation data - will be used to create parks in the database
        # See FUTURE_WORK.md - THRILLWIKI-111 for implementation plan
