import sys
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# Add backend path
sys.path.append(os.path.join(os.path.dirname(__file__), "../../"))

from backend.database import Company, EnrichmentData
from backend.config import settings

def inspect_company(company_name_part):
    engine = create_engine(settings.DATABASE_URL)
    SessionLocal = sessionmaker(bind=engine)
    db = SessionLocal()
    
    try:
        print(f"Searching for company containing: '{company_name_part}'...")
        companies = db.query(Company).filter(Company.name.ilike(f"%{company_name_part}%")).all()
        
        if not companies:
            print("❌ No company found.")
            return

        for company in companies:
            print("\n" + "="*60)
            print(f"🏢 COMPANY: {company.name} (ID: {company.id})")
            print("="*60)
            print(f"🌐 Website: {company.website}")
            print(f"🏗️  Industry (AI): {company.industry_ai}")
            print(f"📊 Metric: {company.calculated_metric_value} {company.calculated_metric_unit} (Std: {company.standardized_metric_value} m²)")
            print(f"✅ Status: {company.status}")
            
            # Enrichment Data
            enrichment = db.query(EnrichmentData).filter(EnrichmentData.company_id == company.id).all()
            print("\n📚 ENRICHMENT DATA:")
            for ed in enrichment:
                print(f"   🔹 Type: {ed.source_type} (Locked: {ed.is_locked})")
                if ed.source_type == "website_scrape":
                    content = ed.content
                    if isinstance(content, dict):
                        summary = content.get("summary", "No summary")
                        raw_text = content.get("raw_text", "")
                        print(f"      📝 Summary: {str(summary)[:200]}...")
                        print(f"      📄 Raw Text Length: {len(str(raw_text))} chars")
                elif ed.source_type == "wikipedia":
                    content = ed.content
                    if isinstance(content, dict):
                        print(f"      🔗 Wiki URL: {content.get('url')}")
                        print(f"      📄 Content Snippet: {str(content.get('full_text', ''))[:200]}...")

    except Exception as e:
        print(f"Error: {e}")
    finally:
        db.close()

if __name__ == "__main__":
    inspect_company("Therme Erding")