
from app import create_app
from app.extensions import db
from sqlalchemy import text, inspect
import os

# Clean SQLite start
if os.path.exists('rateright.db'):
    os.remove('rateright.db')

print('🏗️  COMPLETE RATERIGHT SQLITE TEST')
print('=' * 50)

app = create_app()
with app.app_context():
    try:
        # 1. Database setup
        db.create_all()
        
        # SQLAlchemy 2.x compatible table listing
        inspector = inspect(db.engine)
        tables = inspector.get_table_names()
        
        print(f'✅ Database: {len(tables)} SQLite tables created')
        for table in sorted(tables):
            print(f'   📋 {table}')
        
        # 2. API test
        client = app.test_client()
        response = client.get('/api/health')
        health_data = response.get_json()
        print(f'✅ API health: {response.status_code}')
        print(f'✅ Service: {health_data["service"]}')
        
        # 3. Database engine confirmation
        print(f'✅ Engine: {db.engine.name} (should be sqlite)')
        print(f'✅ Database file: rateright.db')
        
        # 4. Feature test
        features = app.config['FEATURES']
        enabled = sum(1 for f in features.values() if f)
        print(f'✅ Features: {enabled}/{len(features)} enabled')
        
        print('🎉 COMPLETE SQLITE SYSTEM: WORKING!')
        
    except Exception as e:
        print(f'❌ TEST FAILED: {e}')
        import traceback
        traceback.print_exc()
