
import os
import pg8000.dbapi as pg
from lawrisk.utils.env_loader import load_env

def fix_jurisdiction():
    load_env()
    conn_params = {
        "host": os.getenv("LIC_PG_HOST", "172.24.240.1"),
        "port": int(os.getenv("LIC_PG_PORT", "5432")),
        "user": os.getenv("LIC_PG_USER", "postgres"),
        "password": os.getenv("LIC_PG_PASSWORD", ""),
        "database": "licensing_risks",
    }
    
    try:
        conn = pg.connect(**conn_params)
        cur = conn.cursor()
        
        # Get City level region ID
        cur.execute("SELECT id FROM regions WHERE name = '市级'")
        region_row = cur.fetchone()
        if not region_row:
            print("Region '市级' not found.")
            return
        region_id = region_row[0]
        
        # Update jurisdiction_scope to '市级'
        cur.execute("""
            UPDATE region_permit_details 
            SET jurisdiction_scope = '市级'
            WHERE region_id = %s
        """, (region_id,))
        
        count = cur.rowcount
        conn.commit()
        print(f"Updated {count} records in region_permit_details to '市级'.")
        
        conn.close()
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    fix_jurisdiction()
