from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from backend.database import Industry
from backend.services.classification import ClassificationService
import logging

# Setup DB connection directly to the file
# Note: We need to use the absolute path that works inside the container or relative if running locally
# Assuming we run this via 'docker exec' or locally if paths align. 
# For safety, I'll use the path from config but typically inside container it's /app/...
DB_URL = "sqlite:////app/companies_v3_fixed_2.db"

engine = create_engine(DB_URL)
SessionLocal = sessionmaker(bind=engine)
db = SessionLocal()

def test_logic():
    print("--- DEBUGGING STANDARDIZATION LOGIC ---")
    
    industry_name = "Healthcare - Hospital"
    industry = db.query(Industry).filter(Industry.name == industry_name).first()
    
    if not industry:
        print(f"ERROR: Industry '{industry_name}' not found!")
        return

    print(f"Industry: {industry.name}")
    print(f"Search Term: {industry.scraper_search_term}")
    print(f"Standardization Logic (Raw DB Value): '{industry.standardization_logic}'")

    if not industry.standardization_logic:
        print("CRITICAL: Standardization logic is empty/null! That explains the null result.")
        return

    # Initialize Service to test the exact method
    service = ClassificationService()
    test_value = 352.0
    
    print(f"\nTesting calculation with value: {test_value}")
    
    try:
        result = service._parse_standardization_logic(industry.standardization_logic, test_value)
        print(f"Result: {result}")
    except Exception as e:
        print(f"Exception during parsing: {e}")

if __name__ == "__main__":
    test_logic()
