import * as fs from "fs";
import * as path from "path";

/**
 * Quick diagnostic: Check what's happening with Maersk scraping
 * This summarizes the current situation and suggests next steps
 */

async function diagnose() {
  console.log("╔═══════════════════════════════════════════════════╗");
  console.log("║         Maersk Scraping Diagnosis                 ║");
  console.log("╚═══════════════════════════════════════════════════╝\n");

  console.log("Current Situation:");
  console.log("✅ Database connection works");
  console.log("✅ Container MNBU4288732 exists in DB");
  console.log("✅ Tracking URL: https://www.maersk.com/tracking/MNBU4288732");
  console.log("❌ Direct axios request returns HTML shell (no data)");
  console.log("❌ Puppeteer navigation times out");
  console.log("❌ API endpoints are protected or don't exist\n");

  console.log("Analysis:");
  console.log("- Maersk website requires JavaScript to load tracking data");
  console.log("- The page seems to be blocking automated browsers");
  console.log("- Puppeteer timeout suggests aggressive bot detection\n");

  console.log("Options to try (in order):\n");

  console.log("1️⃣  Puppeteer with Stealth Mode (Best option)");
  console.log("   npm install puppeteer-extra puppeteer-extra-plugin-stealth");
  console.log("   npx tsx scripts/test-maersk-stealth.ts\n");

  console.log("2️⃣  Monitor network requests to find API endpoint");
  console.log("   npx tsx scripts/monitor-maersk-requests.ts\n");

  console.log("3️⃣  Check if cached HTML file exists from previous scrape");
  const cacheFiles = [
    "maersk-response.html",
    "maersk-rendered.html",
    "maersk-cached.html",
  ];

  let hasCache = false;
  cacheFiles.forEach((file) => {
    const fullPath = path.join(process.cwd(), file);
    if (fs.existsSync(fullPath)) {
      const size = fs.statSync(fullPath).size;
      console.log(`   ✅ ${file} exists (${size} bytes)`);
      hasCache = true;
    }
  });

  if (!hasCache) {
    console.log(`   ❌ No cached HTML files found\n`);
  } else {
    console.log("");
  }

  console.log("4️⃣  Use Browser DevTools to inspect network");
  console.log("   - Open https://www.maersk.com/tracking/MNBU4288732 in Chrome");
  console.log("   - Press F12 to open DevTools");
  console.log("   - Go to Network tab");
  console.log("   - Look for XHR/Fetch requests that contain tracking data");
  console.log("   - Copy the request URL and test it\n");

  console.log("5️⃣  Try using Playwright instead (different rendering engine)");
  console.log("   npm install playwright");
  console.log("   Create a new script using Playwright\n");

  console.log("6️⃣  As last resort: Capture HTTP requests with curl");
  console.log('   Check if there\'s an API endpoint like /api/tracking/{id}\n');

  console.log("═══════════════════════════════════════════════════\n");

  console.log("Quick test: Let's see what's in the HTML we already got:\n");

  const htmlPath = path.join(process.cwd(), "maersk-response.html");
  if (fs.existsSync(htmlPath)) {
    const html = fs.readFileSync(htmlPath, "utf-8");

    // Extract all unique domains from the HTML
    const urlsRegex = /(?:https?:)?\/\/([a-zA-Z0-9.-]+\.[a-z]{2,})[^"'\s<>]*/g;
    const urls = new Set<string>();
    let match;
    while ((match = urlsRegex.exec(html)) !== null) {
      if (!match[0].includes(".css") && !match[0].includes(".js")) {
        urls.add(match[0]);
      }
    }

    console.log("Potential API domains in HTML:");
    Array.from(urls)
      .filter(
        (url) =>
          url.includes("api") ||
          url.includes("track") ||
          url.includes("graphql") ||
          url.includes("maersk")
      )
      .slice(0, 10)
      .forEach((url) => {
        console.log(`  - ${url}`);
      });
  }
}

diagnose().catch(console.error);
