import sqlite3
import json

DB_PATH = "/app/companies_v3_fixed_2.db"

def inspect(name_part):
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        print(f"Searching for '{name_part}' in {DB_PATH}...")
        cursor.execute("SELECT id, name, website, industry_ai, calculated_metric_value, standardized_metric_value, ai_opener, ai_opener_secondary FROM companies WHERE name LIKE ?", (f'%{name_part}%',))
        companies = cursor.fetchall()
        
        if not companies:
            print("No hits.")
            return

        for c in companies:
            cid, name, website, industry, metric, std_metric, opener_primary, opener_secondary = c
            print("\n" + "="*40)
            print(f"🏢 {name} (ID: {cid})")
            print(f"   Vertical: {industry}")
            print(f"   Website: {website}")
            print(f"   Metric: {metric} (Std: {std_metric})")
            print(f"   Opener (Primary): {opener_primary}")
            print(f"   Opener (Secondary): {opener_secondary}")
            
            # Fetch Enrichment Data
            cursor.execute("SELECT source_type, content FROM enrichment_data WHERE company_id = ?", (cid,))
            rows = cursor.fetchall()
            print("\n   📚 Enrichment Data:")
            for r in rows:
                stype, content_raw = r
                print(f"   - {stype}")
                try:
                    content = json.loads(content_raw)
                    if stype == "website_scrape":
                        summary = content.get("summary", "")
                        raw = content.get("text", "")
                        print(f"     > Summary: {summary[:150]}...")
                        print(f"     > Raw Length: {len(raw)}")
                        if len(raw) > 500:
                             print(f"     > Raw Snippet: {raw[:300]}...")
                    elif stype == "wikipedia":
                        print(f"     > URL: {content.get('url')}")
                        intro = content.get("intro_text", "") or content.get("full_text", "")
                        print(f"     > Intro: {str(intro)[:150]}...")
                except:
                    print("     > (Content not valid JSON)")

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

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