import pg from 'pg';

const pool = new pg.Pool({
  connectionString: 'postgresql://edumanager:EduManager2026!Seguro@150.230.87.131:5432/edumanager'
});

async function run() {
  try {
    console.log('Buscando school_data...');
    const { rows } = await pool.query('SELECT data FROM school_data WHERE id = 1');
    const data = rows[0]?.data;
    if (!data) {
      console.log('school_data não encontrado.');
      return;
    }

    // Recursively search for keys or values containing "webp" or "fotos-alunos"
    const foundPaths = [];
    function search(obj, path = '') {
      if (!obj) return;
      if (typeof obj === 'string') {
        if (obj.includes('webp') || obj.includes('fotos-alunos')) {
          foundPaths.push({ path, value: obj });
        }
      } else if (Array.isArray(obj)) {
        obj.forEach((item, index) => search(item, `${path}[${index}]`));
      } else if (typeof obj === 'object') {
        for (const key of Object.keys(obj)) {
          search(obj[key], `${path}.${key}`);
        }
      }
    }

    search(data);

    console.log('\n--- LINKS ENCONTRADOS NO JSONB ---');
    if (foundPaths.length === 0) {
      console.log('Nenhum link encontrado.');
    } else {
      foundPaths.forEach(p => {
        console.log(`Caminho: ${p.path} | Valor: ${p.value}`);
      });
    }

  } catch (err) {
    console.error(err);
  } finally {
    await pool.end();
  }
}

run();
