import sys
import os
import logging

# Add backend to path
sys.path.append(os.path.join(os.getcwd(), 'backend'))

from backend.database import SessionLocal, Company, Industry
from backend.services.classification import ClassificationService

# Setup basic logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def run_wolfra_reevaluation():
    db = SessionLocal()
    try:
        # Find Wolfra
        company = db.query(Company).filter(Company.name.ilike("%Wolfra%")).first()
        if not company:
            logger.error("Wolfra not found in DB")
            return
        
        industry = db.query(Industry).filter(Industry.name == company.industry_ai).first()
        if not industry:
            logger.error(f"Industry '{company.industry_ai}' for Wolfra not found.")
            return

        logger.info(f"Starting targeted re-evaluation for {company.name} (ID: {company.id})")
        
        classifier = ClassificationService()
        updated_company = classifier.reevaluate_wikipedia_metric(company, db, industry)
        
        logger.info("--- Re-evaluation Result ---")
        print(f"Final Calculated Metric: {updated_company.calculated_metric_value} {updated_company.calculated_metric_unit}")

    finally:
        db.close()

if __name__ == "__main__":
    run_wolfra_reevaluation()
