const { chromium } = require('playwright');
const { upsertMovie } = require('./db');

(async () => {
  const browser = await chromium.launch({ headless: false });
  const allLinks = [];

  // Loop through all pages
  for (let pageNum = 3420; pageNum >= 3415; pageNum--) {
    const page = await browser.newPage();
    const url = `https://mtzy.me/index-${pageNum}.html?ac=detail`;
    await page.goto(url, { waitUntil: 'domcontentloaded' });

    // Scrape the data from this page
    const links = await page.$$eval('tbody tr td a', (links) => {
      return links.map((link) => {
        const href = link.href;
        const title = link.innerText;
        const country = link.closest('tr').querySelector('td:nth-child(2)').innerText;
        const genre = link.closest('tr').querySelector('td:nth-child(3)').innerText;

        // Return the data
        return { href, title, country, genre };
      });
    });
    allLinks.push(...links);
    await page.close();
    console.log(`Scraped page ${pageNum} with ${links.length} movies.`);
  }

  // Visit each link and extract details
  for (let i = 0; i < allLinks.length; i++) {
    const detailPage = await browser.newPage();
    const link = allLinks[i];
    await detailPage.goto(link.href, { waitUntil: 'domcontentloaded' });



    // Extract details and intro from <div class="intro">
    const { clearfixFields, introText } = await detailPage.evaluate(() => {
      const labels = [
        '片名', '别名', '豆瓣', '状态', '类型', '导演', '演员', '年代', '地区', '语言', 'TAG标签', '更新时间'
      ];
      const result = {};
      let introText = '';
      const introDiv = document.querySelector('div.intro');
      if (introDiv) {
        // First clearfix: info list
        const clearfixList = introDiv.querySelector('div.clearfix ul');
        if (clearfixList) {
          const lis = Array.from(clearfixList.querySelectorAll('li'));
          for (const li of lis) {
            for (const label of labels) {
              if (li.textContent.startsWith(label + '：')) {
                result[label] = li.textContent.replace(label + '：', '').trim();
              }
            }
          }
        }
        // Second clearfix: 剧情介绍
        const clearfixDivs = introDiv.querySelectorAll('div.clearfix');
        for (const div of clearfixDivs) {
          const h3 = div.querySelector('h3');
          if (h3 && h3.textContent.includes('剧情介绍')) {
            const p = h3.nextElementSibling;
            if (p && p.tagName === 'P') {
              introText = p.innerText.trim();
            }
          }
        }
      }
      return { clearfixFields: result, introText };
    });


    // Extract image src from <div class='intro'> <img>
    const imageSrc = await detailPage.evaluate(() => {
      const introDiv = document.querySelector('div.intro');
      if (introDiv) {
        const img = introDiv.querySelector('img');
        if (img) return img.src;
      }
      return '';
    });


    // Collect all links containing 'index.m3u8' in href, sorted ascending
    const m3u8_links = await detailPage.$$eval('a[href*="index.m3u8"]', as => as.map(a => a.href));
    // Store as {1: link, 2: link, ...}
    const m3u8_links_obj = {};
    m3u8_links.forEach((link, idx) => {
      m3u8_links_obj[idx + 1] = link;
    });

    // Extract episodes: <dd> tags with <a class='link'>, map episode number to full <a> text content, before <div id='play_2'>
    const episodes = await detailPage.evaluate(() => {
      const result = {};
      const play2Div = document.getElementById('play_2');
      if (!play2Div) return result;
      let node = play2Div.previousElementSibling;
      const dds = [];
      while (node && node.tagName === 'DD') {
        dds.unshift(node);
        node = node.previousElementSibling;
      }
      dds.forEach(dd => {
        const a = dd.querySelector('a.link');
        if (a) {
          const text = a.textContent.trim();
          const epMatch = text.match(/第(\d+)集/);
          if (epMatch) {
            result[parseInt(epMatch[1], 10)] = text;
          }
        }
      });
      return result;
    });

    // Store details in the movie object
    link.details = {
      ...clearfixFields,
      intro: introText,
      image: imageSrc,
      episodes: m3u8_links_obj
    };
    // link.m3u8_links = m3u8_links_obj;

    upsertMovie(link); // Save to SQLite, replace if title exists

    await detailPage.close();
    console.log(`Fetched details for: ${link.title}`);
  }

  // No need to write to result.json
  // await browser.close();
  await browser.close();
})();
