from drugtools.env_setup import ENV,postgres_conn
from psycopg2 import extras
from collections import namedtuple
from tqdm import tqdm


FILES=[
    "../non-db_data_sources/GBD and ICD-10_(2019 version)/NONFATAL_cause2code.psv",
    "../non-db_data_sources/GBD and ICD-10_(2019 version)/COD_cause2code.psv"
]
SEP="|"

sql = """
INSERT INTO "DiseaseBurden".icd10_to_cause
    (code,cause_text)
VALUES %s
"""



with postgres_conn() as pconn, pconn.cursor(cursor_factory=extras.DictCursor) as pcurse:
    entries = []
    for fpath in FILES:
        print(fpath)

        with open(fpath,"r") as fh:
            for line in tqdm(fh.readlines(),desc=fpath):
                code,cause = line.split(SEP)
                code = code.strip()
                cause = cause.strip()

                entries.append((code,cause))
    
    extras.execute_values(pcurse, sql , entries)


