// ข่าวประชาสัมพันธ์
// กิจกรรม

const { execSync } = require("child_process");
const cheerio = require("cheerio");
const fs = require("fs");
const path = require("path");

const BASE = "https://ladsawai.go.th";
// const OUT = path.join(process.cwd(), "scrape_output");
// const OUT = path.join(process.cwd(), "ข่าวประชาสัมพันธ์");
const OUT = path.join(process.cwd(), "กิจกรรม");

fs.mkdirSync(OUT, { recursive: true });

function curlHtml(url) {
  return execSync(
    `curl -L -s "${url}" -H "User-Agent: Mozilla/5.0" -H "Accept-Language: th-TH,th;q=0.9"`,
    { encoding: "utf8", maxBuffer: 20 * 1024 * 1024 }
  );
}

function absUrl(src) {
  if (!src) return null;
  if (src.startsWith("http")) return src;
  return BASE + src;
}

function scrapeOnePage(menuId, page, saveHtml = false) {
  const url = `${BASE}/public/list/data/index/menu/${menuId}/page/${page}`;
  const html = curlHtml(url);

  if (saveHtml) {
    fs.writeFileSync(path.join(OUT, `page-menu-${menuId}-page-${page}.html`), html, "utf8");
  }

  const $ = cheerio.load(html);
  const items = [];

  $(".row.data-row").each((_, row) => {
    const el = $(row);

    const title = el
      .find(".col-sm-8")
      .text()
      .replace(/\s+/g, " ")
      .trim();

    const date = el.find(".col-sm-2").last().text().trim();
    const imgSrc = el.find("img").attr("src");

    if (!title) return;

    items.push({
      title,
      date: date || null,
      image: absUrl(imgSrc),
      sourcePage: page,
      sourceUrl: url,
    });
  });

  const output = {
    source: url,
    scrapedAt: new Date().toISOString(),
    menuId,
    page,
    count: items.length,
    items,
  };

  const outJson = path.join(OUT, `list-menu-${menuId}-page-${page}.json`);
  fs.writeFileSync(outJson, JSON.stringify(output, null, 2), "utf8");

  console.log(`✅ page ${page} -> items ${items.length}`);
  return items;
}

(function main() {
  // const menuId = 1554; // ข่าวประชาสัมพันธ์
  // const totalPages = 53; // ข่าวประชาสัมพันธ์
  const menuId = 1559; // กิจกรรม
  const totalPages = 55;

  const all = [];
  const seen = new Set();

  // ถ้าไม่อยากให้มี HTML 53 ไฟล์ ให้เป็น false
  const saveHtml = false;

  for (let page = 1; page <= totalPages; page++) {
    const items = scrapeOnePage(menuId, page, saveHtml);

    // รวม + กันซ้ำ
    for (const it of items) {
      const key = `${it.title}|${it.date || ""}|${it.image || ""}`;
      if (seen.has(key)) continue;
      seen.add(key);
      all.push(it);
    }
  }

  const merged = {
    menuId,
    totalPages,
    scrapedAt: new Date().toISOString(),
    totalItems: all.length,
    items: all,
  };

  const outAll = path.join(OUT, `list-menu-${menuId}-all.json`);
  fs.writeFileSync(outAll, JSON.stringify(merged, null, 2), "utf8");

  console.log("✅ Saved merged JSON:", outAll);
  console.log("✅ Total unique items:", all.length);
})();
