import sys
import os

# Add the backend directory to the path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

# Set up a mock Flask request context
from flask import Flask
from config import ADMIN_CONFIG

# Create a simple Flask app for testing
app = Flask(__name__)

# Import the business routes
from app.routes.business_routes import business_bp

# Register the blueprint
app.register_blueprint(business_bp)

def test_companies_route():
    with app.test_request_context('/business/companies'):
        # Import the companies function
        from app.routes.business_routes import companies
        
        # Call the companies function
        try:
            result = companies()
            print("Route executed successfully")
            print(f"Result type: {type(result)}")
            # If it's a Response object, we can't easily inspect it
            # But if it's a tuple (template, context), we can
            if isinstance(result, tuple) and len(result) == 2:
                template, context = result
                print(f"Template: {template}")
                print(f"Context keys: {context.keys()}")
                if 'companies' in context:
                    print(f"Companies count: {len(context['companies'])}")
                    for i, company in enumerate(context['companies'][:3]):
                        print(f"  Company {i+1}: {company.company_name}")
        except Exception as e:
            print(f"Error calling companies route: {str(e)}")
            import traceback
            traceback.print_exc()

if __name__ == "__main__":
    test_companies_route()