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

const URL = 'https://www.pvl.cz/portal/nadrze/cz/pc/Mereni.aspx?oid=1&id=VLL1';

async function testPostback() {
  const agent = new https.Agent({ rejectUnauthorized: false });
  const res = await axios.get(URL, { httpsAgent: agent, timeout: 10000 });
  const $ = cheerio.load(res.data);
  
  const viewstate = $('#__VIEWSTATE').val();
  const viewstategenerator = $('#__VIEWSTATEGENERATOR').val();
  const eventvalidation = $('#__EVENTVALIDATION').val();
  
  // Try to POST for monthly data
  const postData = new URLSearchParams();
  postData.append('__EVENTTARGET', 'ctl00$ObsahCPH$PrechodNaBilancniData');
  postData.append('__EVENTARGUMENT', '');
  postData.append('__VIEWSTATE', viewstate as string);
  postData.append('__VIEWSTATEGENERATOR', viewstategenerator as string);
  postData.append('__EVENTVALIDATION', eventvalidation as string);
  
  const postRes = await axios.post(URL, postData.toString(), {
    httpsAgent: agent,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
  
  fs.writeFileSync('pvl_raw_month.html', postRes.data);
  console.log('Saved monthly data to pvl_raw_month.html');
  
  const $post = cheerio.load(postRes.data);
  const rows = $post('table.tabulka-seznam tr:not(:first-child)');
  console.log(`Found ${rows.length} rows in the table.`);
  if (rows.length > 0) {
    const firstRow = rows.first().find('td').first().text().trim();
    const lastRow = rows.last().find('td').first().text().trim();
    console.log(`Date range: ${firstRow} to ${lastRow}`);
  }

}

testPostback().catch(console.error);
