import axios from 'axios';
import * as cheerio from 'cheerio';
import https from 'https';

async function checkLake() {
  const agent = new https.Agent({ rejectUnauthorized: false });
  // Check Lipno 1
  const url = `https://www.pvl.cz/portal/nadrze/cz/pc/Mereni.aspx?id=VLL1&oid=1`;
  try {
    const response = await axios.get(url, {
      httpsAgent: agent,
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
      }
    });
    const $ = cheerio.load(response.data);

    let hasData = false;
    $('table').each((i, tbl) => {
      const firstRowText = $(tbl).find('tr').first().text();
      console.log(`Table ${i} first row:`, firstRowText.trim().replace(/\\s+/g, ' '));
      if (firstRowText.includes('Datum a čas') || firstRowText.includes('Hladina')) {
         hasData = true;
      }
    });
    console.log(`hasData=${hasData}`);
  } catch (err: any) {
    console.error(err.message);
  }
}

checkLake();
