import puppeteer from 'puppeteer';
import fs from 'fs';
import path from 'path';
import { pingDailyPuzzleUpdated } from '../services/indexingService';
import { launchBrowser } from "./puppeteer-launcher";

export async function generateLaCrosswordHints() {
  try {
    const browser = await launchBrowser();
    const page = await browser.newPage();

    // Increase timeout and wait for DOMContentLoaded instead of networkidle0
    await page.goto('https://ladypuzzle.pro/la-times-crossword-clues-answers-hints', {
      waitUntil: 'domcontentloaded',
      timeout: 120000, // 60 seconds
    });

    // Optionally wait for the main crossword container to ensure content is ready
    await page.waitForSelector('.clue-list.clue-list-Across', { timeout: 60000 });

    const crosswordData = await page.evaluate(() => {
      const sections = { across: [], down: [] };

      function scrape(sectionEl: any, targetArray: any[]) {
        if (!sectionEl) return;

        sectionEl.querySelectorAll('li.svelte-h3gg7').forEach((li: any) => {
          const number = li.querySelector('.clue-label.svelte-h3gg7')?.textContent?.trim();
          const clue = li.querySelector('.clue-text.svelte-h3gg7 .mr-1.svelte-h3gg7')?.textContent?.trim();

          const btns = li.querySelectorAll('.clue-btn.svelte-h3gg7');
          const answer = btns[0]?.textContent?.trim();

          if (number && clue && answer) {
            targetArray.push({ number, clue, answer });
          }
        });
      }

      scrape(document.querySelector('.clue-list.clue-list-Across'), sections.across);
      scrape(document.querySelector('.clue-list.clue-list-Down'), sections.down);

      return sections;
    });

    await browser.close();

    const date = new Date().toISOString().split('T')[0];
    const data = { date, ...crosswordData };

    const isProduction = process.env.NODE_ENV === 'production';
    const baseDir = path.join(__dirname, '..', '..', '..');
    const hintsDir = isProduction
      ? path.join(baseDir, 'nytsolvers', 'dist', 'client', 'hints', 'latimescrossword')
      : path.join(baseDir, 'project', 'public', 'hints', 'latimescrossword');

    if (!fs.existsSync(hintsDir)) {
      fs.mkdirSync(hintsDir, { recursive: true });
    }

    fs.writeFileSync(path.join(hintsDir, `${date}.json`), JSON.stringify(data, null, 2));
    console.log('LA Crossword hints generated successfully in', hintsDir);

    // Trigger search engine indexing (Google Indexing API & Bing IndexNow)
    await pingDailyPuzzleUpdated('la-times-crossword-hints', date).catch((err: any) =>
      console.error('[LA Times Crossword] Error pinging search indexing:', err.message)
    );

  } catch (error) {
    console.error('Error generating LA Crossword hints:', error);
  }
}

// Run immediately if script is called directly
if (require.main === module) {
  generateLaCrosswordHints();
}
