import os
import pg8000.dbapi as pg

def load_env():
    env_vars = {}
    try:
        with open('.env', 'r', encoding='utf-8') as f:
            for line in f:
                line = line.strip()
                if not line or line.startswith('#'):
                    continue
                if '=' in line:
                    k, v = line.split('=', 1)
                    env_vars[k.strip()] = v.strip()
    except Exception:
        pass
    return env_vars

env = load_env()
HOST = env.get("LIC_PG_HOST", "172.24.240.1")
PORT = int(env.get("LIC_PG_PORT", "5432"))
USER = env.get("LIC_PG_USER", "postgres")
PASSWORD = env.get("LIC_PG_PASSWORD", "")
DATABASE = env.get("LIC_PG_DATABASE", "licensing_risks")

print(f"Connecting to {HOST}:{PORT}/{DATABASE} as {USER}")
try:
    conn = pg.connect(host=HOST, port=PORT, user=USER, password=PASSWORD, database=DATABASE)
    cursor = conn.cursor()
    
    print("Tables with 'theme' in name:")
    cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name LIKE '%theme%'")
    for row in cursor.fetchall():
        print(f" - {row[0]}")
        
        # Describe table
        print(f"   Columns for {row[0]}:")
        cursor.execute(f"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{row[0]}'")
        for col in cursor.fetchall():
            print(f"     - {col[0]} ({col[1]})")

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