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:
    print("--- ob_procesos_ingredientes ---")
    cur.execute("DESCRIBE ob_procesos_ingredientes")
    for r in cur.fetchall():
        print(f"  {r[0]} ({r[1]})")

    print("\nSample ob_procesos_ingredientes:")
    cur.execute("SELECT * FROM ob_procesos_ingredientes LIMIT 10")
    cols = [d[0] for d in cur.description]
    for r in cur.fetchall():
        print("  ", dict(zip(cols, r)))

    print("\n--- ob_recetas_productos sample ---")
    cur.execute("SELECT rp.*, r.descripcion as receta, a.descripcion as articulo FROM ob_recetas_productos rp JOIN ob_recetas r ON rp.receta_id = r.id LEFT JOIN ge_articulos a ON rp.articulo_id = a.id LIMIT 10")
    cols = [d[0] for d in cur.description]
    for r in cur.fetchall():
        print("  ", dict(zip(cols, r)))

    print("\n--- VIEW v_ob_planificacion_masas_por_receta ---")
    cur.execute("SHOW CREATE VIEW v_ob_planificacion_masas_por_receta")
    print(cur.fetchall())
