import { chromium } from 'playwright';

const baseUrl = (process.env.BASE_URL || 'https://explorer.d-bis.org').replace(/\/$/, '');

const checks = [
  { path: '/', homeVisible: true, expectTexts: ['Gas & Network', 'Latest Blocks', 'Latest Transactions'] },
  { path: '/blocks', activeView: 'blocksView', expectTexts: ['All Blocks'] },
  { path: '/transactions', activeView: 'transactionsView', expectTexts: ['All Transactions'] },
  { path: '/addresses', activeView: 'addressesView', expectTexts: ['All Addresses'] },
  { path: '/tokens', activeView: 'tokensView', expectTexts: ['Tokens'] },
  { path: '/pools', activeView: 'poolsView', expectTexts: ['Pools', 'Canonical PMM routes'] },
  { path: '/routes', activeView: 'routesView', expectTexts: ['Routes', 'Live Route Decision Tree'] },
  { path: '/watchlist', activeView: 'watchlistView', expectTexts: ['Watchlist'] },
  { path: '/bridge', activeView: 'bridgeView', expectTexts: ['Bridge Monitoring'] },
  { path: '/weth', activeView: 'wethView', expectTexts: ['WETH', 'Wrap ETH to WETH9'] },
  { path: '/liquidity', activeView: 'liquidityView', expectTexts: ['Liquidity Access', 'Public Explorer Access Points'] },
  { path: '/more', activeView: 'moreView', expectTexts: ['More', 'Tools & Services'] },
  { path: '/analytics', activeView: 'analyticsView', expectTexts: ['Analytics Dashboard', 'Live Network Analytics'] },
  { path: '/operator', activeView: 'operatorView', expectTexts: ['Operator Panel', 'Operator Access Hub'] },
  { path: '/block/1', activeView: 'blockDetailView', expectTexts: ['Block Details'] },
  {
    path: '/tx/0x0000000000000000000000000000000000000000000000000000000000000000',
    activeView: 'transactionDetailView',
    expectTexts: ['Transaction Details', 'Transaction not found'],
  },
  {
    path: '/address/0x93E66202A11B1772E55407B32B44e5Cd8eda7f22',
    activeView: 'addressDetailView',
    expectTexts: ['Address Details', 'Address'],
  },
];

function hasExpectedText(text, snippets) {
  return snippets.some((snippet) => text.includes(snippet));
}

async function main() {
  const browser = await chromium.launch({ headless: true });
  const page = await browser.newPage();
  let failures = 0;

  for (const check of checks) {
    const url = `${baseUrl}${check.path}`;

    try {
      const response = await page.goto(url, { waitUntil: 'networkidle' });
      if (!response || !response.ok()) {
        console.error(`FAIL ${check.path}: HTTP ${response ? response.status() : 'no-response'}`);
        failures += 1;
        continue;
      }

      const bodyText = await page.textContent('body');
      if (check.homeVisible) {
        const homeVisible = await page.$eval('#homeView', (el) => getComputedStyle(el).display !== 'none').catch(() => false);
        if (!homeVisible) {
          console.error(`FAIL ${check.path}: home view not visible`);
          failures += 1;
          continue;
        }
      } else {
        const activeView = await page.$eval('.detail-view.active', (el) => el.id).catch(() => null);
        if (activeView !== check.activeView) {
          console.error(`FAIL ${check.path}: expected active view ${check.activeView}, got ${activeView}`);
          failures += 1;
          continue;
        }
      }

      if (!hasExpectedText(bodyText || '', check.expectTexts)) {
        console.error(`FAIL ${check.path}: expected one of ${check.expectTexts.join(' | ')}`);
        failures += 1;
        continue;
      }

      console.log(`OK   ${check.path}`);
    } catch (error) {
      console.error(`FAIL ${check.path}: ${error instanceof Error ? error.message : String(error)}`);
      failures += 1;
    }
  }

  await browser.close();

  if (failures > 0) {
    process.exitCode = 1;
    return;
  }

  console.log(`All ${checks.length} route checks passed for ${baseUrl}`);
}

main().catch((error) => {
  console.error(error instanceof Error ? error.stack || error.message : String(error));
  process.exitCode = 1;
});
