from datetime import date

from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.management.base import BaseCommand

from apps.moderation.models import EditSubmission, PhotoSubmission
from apps.parks.models import Park
from apps.rides.models import Ride

User = get_user_model()


class Command(BaseCommand):
    help = "Seeds test submissions for moderation dashboard"

    def handle(self, *args, **kwargs):
        # Ensure we have a test user
        user, created = User.objects.get_or_create(username="test_user", email="test@example.com")
        if created:
            user.set_password("testpass123")
            user.save()
            self.stdout.write(self.style.SUCCESS("Created test user"))

        # Get content types
        park_ct = ContentType.objects.get_for_model(Park)
        ride_ct = ContentType.objects.get_for_model(Ride)

        # Create test park for edit submissions
        test_park, created = Park.objects.get_or_create(
            name="Test Park",
            defaults={
                "description": "A test theme park located in Orlando, Florida",
                "status": "OPERATING",
                "operating_season": "Year-round",
                "size_acres": 100.50,
                "website": "https://testpark.example.com",
            },
        )

        # Create test ride for edit submissions
        test_ride, created = Ride.objects.get_or_create(
            name="Test Coaster",
            park=test_park,
            defaults={
                "description": "A thrilling steel roller coaster with multiple inversions",
                "status": "OPERATING",
                "category": "RC",
                "capacity_per_hour": 1200,
                "ride_duration_seconds": 180,
                "min_height_in": 48,
                "opening_date": date(2020, 6, 15),
            },
        )

        # Create EditSubmissions

        # New park creation with detailed information
        EditSubmission.objects.create(
            user=user,
            content_type=park_ct,
            submission_type="CREATE",
            changes={
                "name": "Adventure World Orlando",
                "description": (
                    "A brand new theme park coming to Orlando featuring five uniquely themed lands: "
                    "Future Frontier, Ancient Mysteries, Ocean Depths, Sky Kingdom, and Fantasy Forest. "
                    "The park will feature state-of-the-art attractions including 3 roller coasters, "
                    "4 dark rides, and multiple family attractions in each themed area."
                ),
                "status": "UNDER_CONSTRUCTION",
                "opening_date": "2024-06-01",
                "operating_season": "Year-round with extended hours during summer and holidays",
                "size_acres": 250.75,
                "website": "https://adventureworld.example.com",
                "location": {
                    "street_address": "1234 Theme Park Way",
                    "city": "Orlando",
                    "state": "Florida",
                    "country": "United States",
                    "postal_code": "32819",
                    "latitude": "28.538336",
                    "longitude": "-81.379234",
                },
            },
            reason=(
                "Submitting new theme park details based on official press release and construction permits. "
                "The park has begun vertical construction and has announced its opening date."
            ),
            source=(
                "Official press release: https://adventureworld.example.com/press/announcement\n"
                "Construction permits: Orange County Building Department #2023-12345"
            ),
            status="PENDING",
        )

        # Existing park edit with comprehensive updates
        EditSubmission.objects.create(
            user=user,
            content_type=park_ct,
            object_id=test_park.id,
            submission_type="EDIT",
            changes={
                "description": (
                    "A world-class theme park featuring 12 uniquely themed areas and over 50 attractions. "
                    'Recent expansion added the new "Cosmic Adventures" area with 2 roller coasters and '
                    "3 family attractions. The park now offers enhanced dining options and night-time "
                    'spectacular "Starlight Dreams".'
                ),
                "status": "OPERATING",
                "website": "https://testpark.example.com",
                "size_acres": 120.25,
                "operating_season": (
                    "Year-round with extended hours (9AM-11PM) during summer. "
                    "Special events during Halloween and Christmas seasons."
                ),
                "location": {
                    "street_address": "5678 Park Boulevard",
                    "city": "Orlando",
                    "state": "Florida",
                    "country": "United States",
                    "postal_code": "32830",
                    "latitude": "28.538336",
                    "longitude": "-81.379234",
                },
            },
            reason=(
                "Updating park information to reflect recent expansion and operational changes. "
                "The new Cosmic Adventures area opened last month and operating hours have been extended."
            ),
            source=(
                "Park press release: https://testpark.example.com/news/expansion\n"
                "Official park map: https://testpark.example.com/map\n"
                "Personal visit and photos from opening day of new area"
            ),
            status="PENDING",
        )

        # New ride creation with detailed specifications
        EditSubmission.objects.create(
            user=user,
            content_type=ride_ct,
            submission_type="CREATE",
            changes={
                "name": "Thunderbolt: The Ultimate Launch Coaster",
                "park": test_park.id,
                "description": (
                    "A cutting-edge steel launch coaster featuring the world's tallest inversion (160 ft) "
                    "and fastest launch acceleration (0-80 mph in 2 seconds). The ride features a unique "
                    "triple launch system, 5 inversions including a zero-g roll and cobra roll, and a "
                    "first-of-its-kind vertical helix element. Total track length is 4,500 feet with a "
                    "maximum height of 375 feet."
                ),
                "status": "UNDER_CONSTRUCTION",
                "category": "RC",
                "opening_date": "2024-07-01",
                "capacity_per_hour": 1400,
                "ride_duration_seconds": 210,
                "min_height_in": 52,
                "manufacturer": 1,  # Assuming manufacturer ID
                "park_area": 1,  # Assuming park area ID
                "stats": {
                    "height_ft": 375,
                    "length_ft": 4500,
                    "speed_mph": 80,
                    "inversions": 5,
                    "propulsion_system": "LSM",
                    "track_material": "STEEL",
                    "roller_coaster_type": "SITDOWN",
                    "trains_count": 3,
                    "cars_per_train": 6,
                    "seats_per_car": 4,
                },
            },
            reason=(
                "Submitting details for the new flagship roller coaster announced by the park. "
                "Construction has begun and track pieces are arriving on site."
            ),
            source=(
                "Official announcement: https://testpark.example.com/thunderbolt\n"
                "Construction photos: https://coasterfan.com/thunderbolt-construction\n"
                "Manufacturer specifications sheet"
            ),
            status="PENDING",
        )

        # Existing ride edit with technical updates
        EditSubmission.objects.create(
            user=user,
            content_type=ride_ct,
            object_id=test_ride.id,
            submission_type="EDIT",
            changes={
                "description": (
                    "A high-speed steel roller coaster featuring 4 inversions and a unique "
                    "dual-loading station system. Recent upgrades include new magnetic braking "
                    "system and enhanced on-board audio experience."
                ),
                "status": "OPERATING",
                "capacity_per_hour": 1500,  # Increased after station upgrades
                "ride_duration_seconds": 185,
                "min_height_in": 48,
                "max_height_in": 80,
                "stats": {
                    "trains_count": 3,
                    "cars_per_train": 8,
                    "seats_per_car": 4,
                },
            },
            reason=(
                "Updating ride information to reflect recent upgrades including new braking system, "
                "audio system, and increased capacity due to improved loading efficiency."
            ),
            source=(
                "Park operations manual\n" "Maintenance records\n" "Personal observation and timing of new ride cycle"
            ),
            status="PENDING",
        )

        # Create PhotoSubmissions with detailed captions

        # Park photo submission
        image_data = (
            b"GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;"
        )
        dummy_image = SimpleUploadedFile("park_entrance.gif", image_data, content_type="image/gif")

        PhotoSubmission.objects.create(
            user=user,
            content_type=park_ct,
            object_id=test_park.id,
            photo=dummy_image,
            caption=(
                "Main entrance plaza of Test Park showing the newly installed digital display board "
                "and renovated ticketing area. Photo taken during morning park opening."
            ),
            date_taken=date(2024, 1, 15),
            status="PENDING",
        )

        # Ride photo submission
        dummy_image2 = SimpleUploadedFile("coaster_track.gif", image_data, content_type="image/gif")
        PhotoSubmission.objects.create(
            user=user,
            content_type=ride_ct,
            object_id=test_ride.id,
            photo=dummy_image2,
            caption=(
                "Test Coaster's first drop and loop element showing the new paint scheme. "
                "Photo taken from the guest pathway near Station Alpha."
            ),
            date_taken=date(2024, 1, 20),
            status="PENDING",
        )

        self.stdout.write(self.style.SUCCESS("Successfully seeded test submissions"))
