import sys
import os

# Add the backend directory to the path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from config import ADMIN_CONFIG
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker

def check_companies():
    try:
        # Database connection
        DATABASE_URL = ADMIN_CONFIG['DATABASE_URL']
        engine = create_engine(DATABASE_URL)
        SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
        
        db = SessionLocal()
        
        # Get total count of companies
        result = db.execute(text('SELECT COUNT(*) FROM companies')).fetchone()
        total_companies = result[0] if result else 0
        print(f"Total companies in database: {total_companies}")
        
        # Get first few companies to see the data
        if total_companies > 0:
            companies = db.execute(text('SELECT company_id, company_name FROM companies LIMIT 5')).fetchall()
            print("\nFirst 5 companies:")
            for company in companies:
                print(f"  - {company[0]}: {company[1]}")
        
        db.close()
        return total_companies
        
    except Exception as e:
        print(f"Error checking companies: {str(e)}")
        return 0

if __name__ == "__main__":
    check_companies()