import os
import sys
import argparse
import logging
from typing import Any # Hinzugefügt

# Add the company-explorer directory to the Python path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))

from backend.database import get_db, Company, Industry, Persona # Added Industry and Persona for full context
from backend.services.classification import ClassificationService
from backend.lib.logging_setup import setup_logging

# --- CONFIGURATION ---
# Setup logging to be very verbose for this script
setup_logging()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

def run_debug_analysis(company_identifier: Any, is_id: bool):
    """
    Runs the full classification and enrichment process for a single company
    in the foreground and prints detailed results.
    """
    logger.info(f"--- Starting Interactive Debug for Company: {company_identifier} (by {'ID' if is_id else 'Name'}) ---")
    
    db_session = next(get_db())
    
    try:
        # 1. Fetch the company
        if is_id:
            company = db_session.query(Company).filter(Company.id == company_identifier).first()
        else:
            company = db_session.query(Company).filter(Company.name == company_identifier).first()
            
        if not company:
            logger.error(f"Company with {'ID' if is_id else 'Name'} {company_identifier} not found.")
            # If by name, suggest similar names
            if not is_id:
                all_company_names = db_session.query(Company.name).limit(20).all()
                print("\nAvailable Company Names (first 20):")
                for (name,) in all_company_names:
                    print(f"- {name}")
            return

        logger.info(f"Found Company: {company.name} (ID: {company.id})")
        
        # --- PRE-ANALYSIS STATE ---
        print("\n--- METRICS BEFORE ---")
        print(f"Calculated: {company.calculated_metric_value} {company.calculated_metric_unit}")
        print(f"Standardized: {company.standardized_metric_value} {company.standardized_metric_unit}")
        print(f"Opener 1 (Infra): {company.ai_opener}")
        print(f"Opener 2 (Ops): {company.ai_opener_secondary}")
        print("----------------------\n")
        
        # 2. Instantiate the service
        classifier = ClassificationService()
        
        # 3. RUN THE CORE LOGIC
        # This will now print all the detailed logs we added
        updated_company = classifier.classify_company_potential(company, db_session)
        
        # --- POST-ANALYSIS STATE ---
        print("\n--- METRICS AFTER ---")
        print(f"Industry (AI): {updated_company.industry_ai}")
        print(f"Metric Source: {updated_company.metric_source}")
        print(f"Proof Text: {updated_company.metric_proof_text}")
        print(f"Calculated: {updated_company.calculated_metric_value} {updated_company.calculated_metric_unit}")
        print(f"Standardized: {updated_company.standardized_metric_value} {updated_company.standardized_metric_unit}")
        print(f"\nOpener 1 (Infra): {updated_company.ai_opener}")
        print(f"Opener 2 (Ops): {updated_company.ai_opener_secondary}")
        print("---------------------")
        
        logger.info(f"--- Interactive Debug Finished for Company: {company.name} (ID: {company.id}) ---")

    except Exception as e:
        logger.error(f"An error occurred during analysis: {e}", exc_info=True)
    finally:
        db_session.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Run a single company analysis for debugging.")
    parser.add_argument("--id", type=int, help="The ID of the company to analyze.")
    parser.add_argument("--company-name", type=str, help="The name of the company to analyze.")
    args = parser.parse_args()
    
    if args.id and args.company_name:
        parser.error("Please provide either --id or --company-name, not both.")
    elif args.id:
        run_debug_analysis(args.id, is_id=True)
    elif args.company_name:
        run_debug_analysis(args.company_name, is_id=False)
    else:
        parser.error("Please provide either --id or --company-name.")
