#!/usr/bin/env python3
"""
Test script to verify that the updated models and schemas can be imported successfully.
"""

import sys
from pathlib import Path

# Add the backend directory to the path
sys.path.append(str(Path(__file__).parent / 'backend'))

def test_model_imports():
    """Test that the updated models can be imported without errors."""
    print("Testing model imports...")
    
    try:
        from app.models.business import Market, Adventure
        print("✅ Successfully imported Market and Adventure models")
        
        # Test that the new attributes exist on the models
        market_attrs = dir(Market)
        adventure_attrs = dir(Adventure)
        
        new_columns = ['address', 'city', 'country_id', 'latitude', 'longitude', 'phone', 'email', 'website', 'updated_at']
        
        print("\nChecking Market model for new columns:")
        for col in new_columns:
            if col in market_attrs:
                print(f"  ✅ {col}: Found")
            else:
                print(f"  ❌ {col}: Missing")
        
        print("\nChecking Adventure model for new columns:")
        for col in new_columns:
            if col in adventure_attrs:
                print(f"  ✅ {col}: Found") 
            else:
                print(f"  ❌ {col}: Missing")
                
    except Exception as e:
        print(f"❌ Error importing models: {e}")
        return False
    
    return True

def test_schema_imports():
    """Test that the updated schemas can be imported without errors."""
    print("\nTesting schema imports...")
    
    try:
        from app.schemas.business import Market, Adventure, MarketCreate, MarketUpdate, AdventureCreate, AdventureUpdate
        print("✅ Successfully imported Market and Adventure schemas")
        
        # Test that we can create instances (basic validation)
        try:
            # Test basic Market schema fields
            market_fields = Market.__fields__.keys()
            adventure_fields = Adventure.__fields__.keys()
            
            new_fields = ['address', 'city', 'country_id', 'latitude', 'longitude', 'phone', 'email', 'website', 'updated_at']
            
            print("\nChecking Market schema for new fields:")
            for field in new_fields:
                if field in market_fields:
                    print(f"  ✅ {field}: Found")
                else:
                    print(f"  ❌ {field}: Missing")
            
            print("\nChecking Adventure schema for new fields:")
            for field in new_fields:
                if field in adventure_fields:
                    print(f"  ✅ {field}: Found")
                else:
                    print(f"  ❌ {field}: Missing")
                    
        except Exception as e:
            print(f"❌ Error checking schema fields: {e}")
            return False
            
    except Exception as e:
        print(f"❌ Error importing schemas: {e}")
        return False
    
    return True

def test_model_instantiation():
    """Test that models can be instantiated with new fields."""
    print("\nTesting model instantiation...")
    
    try:
        from app.models.business import Market, Adventure
        import uuid
        from decimal import Decimal
        
        # Test Market instantiation
        market = Market(
            market_name="Test Market",
            address="123 Test Street",
            city="Test City", 
            country_id=1,
            latitude=Decimal('37.7749'),
            longitude=Decimal('-122.4194'),
            phone="+1234567890",
            email="test@market.com",
            website="https://testmarket.com"
        )
        print("✅ Market model instantiation successful")
        
        # Test Adventure instantiation
        adventure = Adventure(
            adventure_name="Test Adventure",
            price=Decimal('99.99'),
            address="456 Adventure Lane",
            city="Adventure City",
            country_id=1,
            latitude=Decimal('37.7849'),
            longitude=Decimal('-122.4094'),
            phone="+1987654321",
            email="test@adventure.com",
            website="https://testadventure.com"
        )
        print("✅ Adventure model instantiation successful")
        
    except Exception as e:
        print(f"❌ Error testing model instantiation: {e}")
        return False
    
    return True

if __name__ == "__main__":
    print("🧪 TESTING UPDATED BACKEND MODELS AND SCHEMAS")
    print("=" * 60)
    
    success = True
    success &= test_model_imports()
    success &= test_schema_imports() 
    success &= test_model_instantiation()
    
    print("\n" + "=" * 60)
    if success:
        print("✅ ALL TESTS PASSED!")
        print("The backend models and schemas have been successfully updated.")
        print("\nNew columns added to both Market and Adventure:")
        print("  • address (VARCHAR(255))")
        print("  • city (VARCHAR(100))")
        print("  • country_id (BIGINT, FK to countries)")
        print("  • latitude (DECIMAL(10,8))")
        print("  • longitude (DECIMAL(11,8))")
        print("  • phone (VARCHAR(50))")
        print("  • email (VARCHAR(255))")
        print("  • website (VARCHAR(500))")
        print("  • updated_at (TIMESTAMP with auto-update)")
    else:
        print("❌ SOME TESTS FAILED")
        print("Please check the error messages above and fix the issues.")