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():
    conn = get_conn()
    cur = conn.cursor()
    
    print("Checking for duplicate region_permit_details (same region, permit)...")
    cur.execute("""
        SELECT region_id, permit_id, COUNT(*)
        FROM region_permit_details
        GROUP BY region_id, permit_id
        HAVING COUNT(*) > 1
    """)
    rows = cur.fetchall()
    print(f"Duplicate combinations in region_permit_details: {len(rows)}")
    for r in rows:
        print(f" R:{r[0]} P:{r[1]} Count:{r[2]}")

    conn.close()

if __name__ == "__main__":
    check()
