#!/usr/bin/env python
"""
Test script to verify manufacturer syncing with ride models.
"""
import os
import sys
import django

# Add the backend directory to the Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend'))

# Set up Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local')
django.setup()

from apps.rides.models import Ride, RideModel, Company
from apps.parks.models import Park

def test_manufacturer_sync():
    """Test that ride manufacturer syncs with ride model manufacturer."""
    
    print("Testing manufacturer sync with ride models...")
    
    # Get a ride that has a ride_model
    ride = Ride.objects.select_related('ride_model', 'ride_model__manufacturer', 'manufacturer').first()
    
    if not ride:
        print("No rides found in database")
        return
    
    print(f"\nRide: {ride.name}")
    print(f"Park: {ride.park.name}")
    
    if ride.ride_model:
        print(f"Ride Model: {ride.ride_model.name}")
        print(f"Ride Model Manufacturer: {ride.ride_model.manufacturer.name if ride.ride_model.manufacturer else 'None'}")
    else:
        print("Ride Model: None")
    
    print(f"Ride Manufacturer: {ride.manufacturer.name if ride.manufacturer else 'None'}")
    
    # Check if they match
    if ride.ride_model and ride.ride_model.manufacturer:
        if ride.manufacturer == ride.ride_model.manufacturer:
            print("✅ Manufacturer is correctly synced with ride model")
        else:
            print("❌ Manufacturer is NOT synced with ride model")
            print("This should be fixed by the new save() method")
            
            # Test the fix by saving the ride
            print("\nTesting fix by re-saving the ride...")
            ride.save()
            ride.refresh_from_db()
            
            print(f"After save - Ride Manufacturer: {ride.manufacturer.name if ride.manufacturer else 'None'}")
            
            if ride.manufacturer == ride.ride_model.manufacturer:
                print("✅ Fix successful! Manufacturer is now synced")
            else:
                print("❌ Fix failed - manufacturer still not synced")
    else:
        print("⚠️  Cannot test sync - ride model has no manufacturer")

if __name__ == "__main__":
    test_manufacturer_sync()
