import pymysql, configparser

def decrypt(s):
    if not s or not s.startswith('ENC:'): return s
    hex_str = s[4:]
    key = 0xA5C3
    res = []
    for i in range(0, len(hex_str), 4):
        val = int(hex_str[i:i+4], 16)
        res.append(chr(val ^ key))
    return ''.join(res)

cfg = configparser.ConfigParser()
cfg.read('C:/GESTIGEST/db_config.ini')
server = decrypt(cfg['Database'].get('Server'))
port = int(decrypt(cfg['Database'].get('Port')))
db = decrypt(cfg['Database'].get('Database'))
user = decrypt(cfg['Database'].get('User_Name'))
pwd = decrypt(cfg['Database'].get('Password'))

conn = pymysql.connect(host=server, port=port, user=user, password=pwd, database=db, charset='utf8mb4')
with conn.cursor() as cur:
    for tbl in ['ge_albaranes', 'ge_albaranes_lineas', 'ge_albaranes_bultos', 'ge_envios', 'ge_envios_recogidas', 'ge_envios_expediciones']:
        cur.execute(f"SELECT column_name FROM information_schema.columns WHERE table_schema='{db}' AND table_name='{tbl}'")
        cols = [c[0] for c in cur.fetchall()]
        print(f"Columnas de {tbl}: {cols}")
