from lawrisk.utils.env_loader import load_env
load_env()

import os
import pg8000.dbapi as pg

def get_conn():
    host = os.getenv("LIC_PG_HOST", "172.24.240.1")
    port = int(os.getenv("LIC_PG_PORT", os.getenv("PG_PORT", "5432")))
    user = os.getenv("LIC_PG_USER", os.getenv("PG_USER", "postgres"))
    password = os.getenv("LIC_PG_PASSWORD", "")
    database = os.getenv("LIC_PG_DATABASE", "licensing_risks")
    return pg.connect(host=host, port=port, user=user, password=password, database=database)

def check_schema():
    conn = get_conn()
    cur = conn.cursor()
    
    print("--- Constraints for table 'risks' ---")
    cur.execute("""
        SELECT conname, pg_get_constraintdef(c.oid)
        FROM pg_constraint c
        JOIN pg_class t ON t.oid = c.conrelid
        WHERE t.relname = 'risks'
    """)
    for row in cur.fetchall():
        print(f" {row[0]}: {row[1]}")
        
    print("\n--- Indices for table 'risks' ---")
    cur.execute("""
        SELECT indexname, indexdef
        FROM pg_indexes
        WHERE tablename = 'risks'
    """)
    for row in cur.fetchall():
        print(f" {row[0]}: {row[1]}")

    conn.close()

if __name__ == "__main__":
    check_schema()
