const fs = require('fs/promises');
const path = require('path');
const cheerio = require('cheerio');
const config = require('../src/config');
const { launchBrowser, createContext } = require('../src/session');
const { fetchTargetHtmlWithSession, buildForecastUrl } = require('../src/fetcher');
const { parseItemsFromHtml, countForecastRows } = require('../src/parser');
const { sanitizeFilePart } = require('../src/utils');

async function main() {
  const botArg = process.argv[2] || 'raketafon';
  const activeTab = Number(process.argv[3] || 1);
  const currentPage = Number(process.argv[4] || 1);
  const botUrl = botArg.startsWith('http') ? botArg : `https://alpinbet.com/dispatch/antigol/${botArg}`;
  const targetUrl = buildForecastUrl(botUrl, {
    activeTab,
    perPage: config.forecastPerPage,
    page: currentPage
  });

  const browser = await launchBrowser({ headless: true });
  const context = await createContext({ browser, sessionFile: config.sessionFile });

  try {
    const result = await fetchTargetHtmlWithSession({ context, config, targetUrl });
    const items = parseItemsFromHtml(result.html, config.selectors);
    const rows = countForecastRows(result.html, config.selectors);
    const $ = cheerio.load(result.html);
    const htmlFile = path.join(
      config.htmlSnapshotDir,
      `${sanitizeFilePart(botArg)}_tab-${activeTab}_page-${currentPage}_manual-debug.html`
    );

    await fs.mkdir(config.htmlSnapshotDir, { recursive: true });
    await fs.writeFile(htmlFile, result.html, 'utf8');

    console.log('status:', result.status);
    console.log('title:', result.title);
    console.log('url:', result.finalUrl);
    console.log('rows:', rows);
    console.log('parsedItems:', items.length);
    console.log('htmlFile:', htmlFile);

    const rowCandidates = $('.table-link')
      .slice(0, 5)
      .toArray()
      .map((element, index) => {
        const $row = $(element);
        return {
          index,
          classes: $row.attr('class') || '',
          text: $row.text().replace(/\s+/g, ' ').trim().slice(0, 200),
          hasLink: $row.find('.cell-team-title a').length > 0,
          hasFavorite: $row.find('.rating-mailings__favorite').length > 0,
          hasCoefficient: $row.find('.cell-coefficient__total, .cell-coefficient').length > 0
        };
      });

    console.log('tableLinkCandidates:', JSON.stringify(rowCandidates, null, 2));
    if (items[0]) {
      console.log('firstItem:', JSON.stringify(items[0], null, 2));
    }
  } finally {
    await browser.close();
  }
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});
