const { Client } = require('ssh2');
const conn = new Client();
function exec(conn, cmd) {
  return new Promise((resolve, reject) => {
    conn.exec(cmd, (err, stream) => {
      if (err) return reject(err);
      let out = '', e = '';
      stream.on('data', d => out += d);
      stream.stderr.on('data', d => e += d);
      stream.on('close', () => resolve({ out: out.trim(), err: e.trim() }));
    });
  });
}
conn.on('ready', async () => {
  const r1 = await exec(conn, 'ls /home/slin/enduro-trails/data/terrain/hypso/8/ | head -5');
  console.log('hypso/8/ dirs:', r1.out || r1.err);

  const r2 = await exec(conn, 'ls /home/slin/enduro-trails/data/terrain/hypso/8/75/ 2>/dev/null | head -5');
  console.log('hypso/8/75/ files:', r2.out || r2.err);

  const r3 = await exec(conn, 'docker exec prototype-enduro-trails-1 ls /app/data/ 2>/dev/null');
  console.log('container /app/data:', r3.out || r3.err || 'empty');

  const r4 = await exec(conn, 'docker inspect prototype-enduro-trails-1 --format "{{range .Mounts}}{{.Source}}->{{.Destination}} {{end}}"');
  console.log('mounts:', r4.out || r4.err);

  const r5 = await exec(conn, 'docker exec prototype-enduro-trails-1 python3 -c "import os; p=os.path.join(os.path.dirname(\'/app/app.py\'), \'../data/terrain\'); print(p); print(os.path.exists(p))"');
  console.log('TERRAIN_DIR:', r5.out || r5.err);

  const r6 = await exec(conn, 'curl -s http://localhost:5558/api/health');
  console.log('health:', r6.out || r6.err);

  conn.end();
});
conn.on('error', err => { console.error('error:', err.message); process.exit(1); });
conn.connect({ host: '82.22.50.71', username: 'slin', password: 'motoZ@yaz2010', readyTimeout: 15000 });
