/**
 * ============================================================
 *  Simple Web Scraper — Node.js + Playwright
 *  File   : scraper.js
 *  Output : data_scraping.csv
 * ============================================================
 *
 *  INSTALASI:
 *    1. Pastikan Node.js sudah terinstall (https://nodejs.org)
 *    2. Buka terminal di folder ini, lalu jalankan:
 *         npm install playwright
 *         npx playwright install chromium
 *
 *  MENJALANKAN:
 *         node scraper.js
 *
 *  Catatan: Ganti nilai TARGET_URL dan SELECTOR sesuai kebutuhan.
 * ============================================================
 */

const { chromium } = require('playwright');
const fs = require('fs');
const path = require('path');

// ── ① Konfigurasi — ubah sesuai kebutuhan ───────────────────
const TARGET_URL = 'https://x.com/Lonewolf48__';          // URL target
const WAIT_FOR = 'article[data-testid="tweet"]';         // Selector tweet X.com
const OUTPUT_FILE = path.join(__dirname, 'data_scraping.csv');
const SEPARATOR = ';'; // Gunakan ';' agar Excel Indonesia langsung terbaca

// ── Cookie login X.com (WAJIB diisi agar tidak kena login wall) ──────
// Cara ambil auth_token:
//   1. Login ke x.com di browser Chrome/Firefox
//   2. Buka DevTools → Application → Cookies → https://x.com
//   3. Salin nilai cookie bernama "auth_token" → tempelkan di bawah
const X_AUTH_TOKEN = '5e114136ed52971adddd375366103dafa7044864';
// ─────────────────────────────────────────────────────────────────────

/**
 * Mengubah string agar aman disimpan di dalam sel CSV.
 * Jika ada koma, semicolon, tanda kutip, atau newline → bungkus dengan "..."
 */
function escapeCSV(value) {
  if (value === null || value === undefined) return '';
  const str = String(value).trim();
  if (str.includes(SEPARATOR) || str.includes('"') || str.includes('\n')) {
    return '"' + str.replace(/"/g, '""') + '"';
  }
  return str;
}

/**
 * Menyimpan array-of-objects ke file CSV.
 * Baris pertama = header (key dari object pertama).
 */
function saveToCSV(records, filePath) {
  if (records.length === 0) {
    console.log('⚠  Tidak ada data yang berhasil di-scrape.');
    return;
  }

  const headers = Object.keys(records[0]);
  const headerLine = headers.map(escapeCSV).join(SEPARATOR);

  const rows = records.map(record =>
    headers.map(h => escapeCSV(record[h])).join(SEPARATOR)
  );

  // BOM (EF BB BF) → agar Excel/Spreadsheet langsung mengenali UTF-8
  const bom = '\uFEFF';
  const content = bom + [headerLine, ...rows].join('\r\n');

  fs.writeFileSync(filePath, content, { encoding: 'utf8' });
  console.log(`✅ Data berhasil disimpan ke: ${filePath}`);
  console.log(`   Total baris data : ${records.length}`);
}

/**
 * Fungsi utama — menjalankan seluruh alur scraping.
 */
async function runScraper() {
  console.log('🚀 Memulai proses scraping...');
  console.log(`   Target URL : ${TARGET_URL}`);
  console.log(`   Selector   : ${WAIT_FOR}`);
  console.log('');

  // ── ② Inisialisasi browser (headless = hemat memori) ──────
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({
    userAgent:
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' +
      'AppleWebKit/537.36 (KHTML, like Gecko) ' +
      'Chrome/124.0.0.0 Safari/537.36',
    locale: 'id-ID',
    viewport: { width: 1280, height: 800 },
  });

  // ── Inject cookie auth_token agar X.com tidak redirect ke login ──
  if (!X_AUTH_TOKEN || X_AUTH_TOKEN === 'PASTE_AUTH_TOKEN_KAMU_DI_SINI') {
    console.error('❌ X_AUTH_TOKEN belum diisi! Baca komentar di bagian konfigurasi.');
    await browser.close();
    process.exit(1);
  }
  await context.addCookies([
    { name: 'auth_token', value: X_AUTH_TOKEN, domain: '.x.com', path: '/' },
  ]);

  const page = await context.newPage();

  let records = [];

  try {
    // ── ③ Buka halaman & tunggu elemen ────────────────────────
    console.log('📄 Membuka halaman...');
    await page.goto(TARGET_URL, { waitUntil: 'domcontentloaded', timeout: 30_000 });

    console.log(`⏳ Menunggu elemen "${WAIT_FOR}"...`);
    await page.waitForSelector(WAIT_FOR, { timeout: 15_000 });
    console.log('   Elemen ditemukan!\n');

    // ── ④ Ekstraksi data tweet dari X.com ────────────────────
    records = await page.evaluate((selector) => {
      const items = document.querySelectorAll(selector);
      const extracted = [];

      items.forEach(item => {
        // Teks tweet
        const teksEl = item.querySelector('[data-testid="tweetText"]');
        const teks = teksEl ? teksEl.innerText.trim() : '';

        // Link permanen tweet
        const timeEl = item.querySelector('time');
        const href = timeEl && timeEl.parentElement
          ? timeEl.parentElement.href
          : '';

        // Jumlah like
        const likeEl = item.querySelector('[data-testid="like"] span');
        const like = likeEl ? likeEl.innerText.trim() : '0';

        // Jumlah retweet
        const rtEl = item.querySelector('[data-testid="retweet"] span');
        const rt = rtEl ? rtEl.innerText.trim() : '0';

        if (teks) {
          extracted.push({ teks, tautan: href, like, retweet: rt });
        }
      });

      return extracted;
    }, WAIT_FOR);

    // ── ⑤ Tambahkan timestamp ke setiap baris ─────────────────
    const now = new Date().toLocaleString('id-ID', {
      timeZone: 'Asia/Jakarta',
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
    });

    records = records.map(r => ({ ...r, scraped_at: now }));

    console.log(`🔍 Total item yang ditemukan: ${records.length}`);

    // Preview 3 item pertama
    if (records.length > 0) {
      console.log('\n📋 Preview 3 item pertama:');
      records.slice(0, 3).forEach((r, i) => {
        console.log(`   [${i + 1}] ${r.teks}`);
        console.log(`       URL   : ${r.tautan}`);
        console.log(`       Skor  : ${r.skor}`);
        console.log(`       Waktu : ${r.scraped_at}`);
      });
      console.log('');
    }

  } catch (err) {
    console.error('❌ Terjadi kesalahan saat scraping:', err.message);
  } finally {
    // ── ⑥ Tutup browser ───────────────────────────────────────
    await browser.close();
    console.log('🔒 Browser ditutup.\n');
  }

  // ── ⑦ Simpan ke CSV ───────────────────────────────────────
  saveToCSV(records, OUTPUT_FILE);
}

// ── Jalankan ────────────────────────────────────────────────
runScraper().catch(err => {
  console.error('❌ Fatal error:', err);
  process.exit(1);
});
