import sqlite3
import os

DB_PATH = "companies_v3_fixed_2.db"

def check_company():
    if not os.path.exists(DB_PATH):
        print(f"❌ Database not found at {DB_PATH}")
        return

    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        print(f"🔍 Searching for 'Silly Billy' in {DB_PATH}...")
        cursor.execute("SELECT id, name, crm_id, ai_opener, ai_opener_secondary, city, crm_vat, status FROM companies WHERE name LIKE '%Silly Billy%'")
        rows = cursor.fetchall()
        
        if not rows:
            print("❌ No company found matching 'Silly Billy'")
        else:
            for row in rows:
                company_id = row[0]
                print("\n✅ Company Found:")
                print(f"  ID: {company_id}")
                print(f"  Name: {row[1]}")
                print(f"  CRM ID: {row[2]}")
                print(f"  Status: {row[7]}")
                print(f"  City: {row[5]}")
                print(f"  VAT: {row[6]}")
                print(f"  Opener (Primary): {row[3][:50]}..." if row[3] else "  Opener (Primary): None")
                
                # Check Enrichment Data
                print(f"\n  🔍 Checking Enrichment Data for ID {company_id}...")
                cursor.execute("SELECT content FROM enrichment_data WHERE company_id = ? AND source_type = 'website_scrape'", (company_id,))
                enrich_row = cursor.fetchone()
                if enrich_row:
                    import json
                    try:
                        data = json.loads(enrich_row[0])
                        imp = data.get("impressum")
                        print(f"  Impressum Data in Scrape: {json.dumps(imp, indent=2) if imp else 'None'}")
                    except Exception as e:
                        print(f"  ❌ Error parsing JSON: {e}")
                else:
                    print("  ❌ No website_scrape enrichment data found.")
            
        conn.close()
    except Exception as e:
        print(f"❌ Error reading DB: {e}")

if __name__ == "__main__":
    check_company()
