import sys
sys.stdout.reconfigure(encoding='utf-8')
import sqlite3
import json
import os

db_path = os.path.join("database", "canifa_ai_dump.sqlite")
print(f"Checking DB: {db_path}")

try:
    conn = sqlite3.connect(db_path)
    cur = conn.cursor()
    cur.execute("PRAGMA table_info(pg__dashboard_canifa__ultra_descriptions);")
    columns = cur.fetchall()
    print("Columns:", [c[1] for c in columns])
    
    cur.execute("SELECT base_ref_code, tags, description_data FROM pg__dashboard_canifa__ultra_descriptions WHERE tags IS NOT NULL AND tags != '' LIMIT 1;")
    row = cur.fetchone()
    
    if row:
        product_code = row[0]
        tags = row[1]
        desc_data = row[2]
        
        print("--- BEFORE UPDATE ---")
        print("Product Code:", product_code)
        print("Tags (Standalone):", tags)
        print("Desc Data JSON:", desc_data)
        
        # Parse it
        try:
            parsed_desc = json.loads(desc_data)
            parsed_tags = json.loads(tags)
            
            # Simulate update
            parsed_desc["tags"] = parsed_tags
            
            print("\n--- AFTER UPDATE (PREVIEW) ---")
            print("New Desc Data JSON:", json.dumps(parsed_desc, ensure_ascii=False, indent=2))
        except Exception as json_err:
            print("JSON Parse Error:", json_err)
            
    else:
        print("No rows found with tags.")
        
    cur.close()
    conn.close()
except Exception as e:
    print("DB Error:", e)
