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

const ALL_LAKES = [
  {"href": "Mereni.aspx?id=BIBI&oid=1", "text": "VD Bílsko"},
  {"href": "Mereni.aspx?id=RACU&oid=3", "text": "VD České Údolí"},
  {"href": "Mereni.aspx?id=KLDP&oid=3", "text": "VD Dolejší Padrťský rybník"},
  {"href": "Mereni.aspx?id=CPDR&oid=3", "text": "VD Dráteník"},
  {"href": "Mereni.aspx?id=KLHP&oid=3", "text": "VD Hořejší Padrťský rybník"},
  {"href": "Mereni.aspx?id=SCHU&oid=1", "text": "VD Humenice"},
  {"href": "Mereni.aspx?id=BLHU&oid=1", "text": "VD Husinec"},
  {"href": "Mereni.aspx?id=VLKA&oid=2", "text": "VD Kamýk"},
  {"href": "Mereni.aspx?id=SPKA&oid=1", "text": "VD Karhof"},
  {"href": "Mereni.aspx?id=KLKL&oid=3", "text": "VD Klabava"},
  {"href": "Mereni.aspx?id=KCKC&oid=3", "text": "VD Klíčava"},
  {"href": "Mereni.aspx?id=LILA&oid=3", "text": "VD Láz"},
  {"href": "Mereni.aspx?id=MZLU&oid=3", "text": "VD Lučina"},
  {"href": "Mereni.aspx?id=SPNE&oid=2", "text": "VD Němčice"},
  {"href": "Mereni.aspx?id=UHNY&oid=3", "text": "VD Nýrsko"},
  {"href": "Mereni.aspx?id=OPOB&oid=3", "text": "VD Obecnice"},
  {"href": "Mereni.aspx?id=PPPI&oid=3", "text": "VD Pilská (u Příbramě)"},
  {"href": "Mereni.aspx?id=SAPI&oid=2", "text": "VD Pilská u Žďáru"},
  {"href": "Mereni.aspx?id=HESE&oid=2", "text": "VD Sedlice"},
  {"href": "Mereni.aspx?id=CRSO&oid=1", "text": "VD Soběnov"},
  {"href": "Mereni.aspx?id=SVSV&oid=2", "text": "VD Staviště"},
  {"href": "Mereni.aspx?id=STST&oid=2", "text": "VD Strž"},
  {"href": "Mereni.aspx?id=SMSM&oid=3", "text": "VD Suchomasty"},
  {"href": "Mereni.aspx?id=ZESV&oid=2", "text": "VD Švihov (Želivka)"},
  {"href": "Mereni.aspx?id=TRTR&oid=2", "text": "VD Trnávka"},
  {"href": "Mereni.aspx?id=VLVE&oid=2", "text": "VD Vrané"},
  {"href": "Mereni.aspx?id=HEVR&oid=2", "text": "VD Vřesník"},
  {"href": "Mereni.aspx?id=CPZA&oid=3", "text": "VD Záskalská"},
  {"href": "Mereni.aspx?id=SPZH&oid=1", "text": "VD Zhejral"},
  {"href": "Mereni.aspx?id=STZL&oid=3", "text": "VD Žlutice"}
];

async function checkLakes() {
  const agent = new https.Agent({ rejectUnauthorized: false });
  const validLakes: any[] = [];

  for (const lake of ALL_LAKES) {
    const url = `https://www.pvl.cz/portal/nadrze/cz/pc/${lake.href}`;
    try {
      const response = await axios.get(url, {
        httpsAgent: agent,
        headers: { 'User-Agent': 'Mozilla/5.0' }
      });
      const $ = cheerio.load(response.data);
      let hasHistory = false;
      let hasInflow = false;

      $('table').each((i, tbl) => {
        const text = $(tbl).text();
        if (text.includes('Aktuální hodnoty') && text.includes('Přítok')) {
           hasInflow = true;
        }
        if (text.includes('Datum') && text.includes('Odtok')) {
           const rows = $(tbl).find('tr').length;
           if (rows > 2) hasHistory = true;
        }
      });

      if (hasHistory && hasInflow) {
        validLakes.push(lake);
        console.log(`[VALID] ${lake.text}`);
      } else {
        console.log(`[INVALID] ${lake.text} (Hist:${hasHistory}, In:${hasInflow})`);
      }
    } catch (err: any) {
      console.error(`[ERROR] ${lake.text}: ${err.message}`);
    }
  }
  
  console.log('\\n--- SUMMARY OF VALID LAKES ---');
  console.log(JSON.stringify(validLakes, null, 2));
}

checkLakes();
