/**
 * SFPcables.com Scraper — 10Gtek's Retail Store
 *
 * sfpcables.com — Magento store with server-rendered HTML, real USD prices.
 * Product pages have clean <h2 class="product-name"> + <span class="price"> structure.
 * Rate limited: 1 req/2sec. TLS verification disabled (self-signed cert issues).
 */
import { pool, findOrCreateScrapedTransceiver, ensureVendor, upsertPriceObservation } from "../utils/db";
import { contentHash } from "../utils/hash";

const BASE = "https://www.sfpcables.com";
const HEADERS = {
  "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
  Accept: "text/html,application/xhtml+xml",
};

const CATEGORIES = [
  { path: "/sfp-1-25g-series", formFactor: "SFP", speed: "1G", speedGbps: 1 },
  { path: "/sfp-transceivers", formFactor: "SFP+", speed: "10G", speedGbps: 10 },
  { path: "/sfp28-transceivers", formFactor: "SFP28", speed: "25G", speedGbps: 25 },
  { path: "/qsfp-transceivers", formFactor: "QSFP+", speed: "40G", speedGbps: 40 },
  { path: "/100g-qsfp28-transceivers", formFactor: "QSFP28", speed: "100G", speedGbps: 100 },
  { path: "/qsfp-dd-400g-transceivers", formFactor: "QSFP-DD", speed: "400G", speedGbps: 400 },
  { path: "/xfp-transceivers", formFactor: "XFP", speed: "10G", speedGbps: 10 },
  { path: "/2-5g-transceivers", formFactor: "SFP", speed: "2.5G", speedGbps: 2.5 },
  { path: "/industrial-sfp-transceivers", formFactor: "SFP", speed: "1G", speedGbps: 1 },
  { path: "/industrial-qsfp-transceivers", formFactor: "QSFP28", speed: "100G", speedGbps: 100 },
  { path: "/8x50g-qsfp-dd-transceiver-optical-module", formFactor: "QSFP-DD", speed: "400G", speedGbps: 400 },
  { path: "/8x100g-qsfp-dd-transceiver-optical-module", formFactor: "QSFP-DD", speed: "800G", speedGbps: 800 },
  { path: "/osfp-flat-fiber-optic-transceiver-modules", formFactor: "OSFP", speed: "400G", speedGbps: 400 },
  { path: "/400g-8x50g-osfp-fin-fiber-optic-transceiver-modules", formFactor: "OSFP", speed: "400G", speedGbps: 400 },
  { path: "/fc16g-sfp-transceivers", formFactor: "SFP+", speed: "16G FC", speedGbps: 16 },
  { path: "/fc32g-sfp-transceivers", formFactor: "SFP28", speed: "32G FC", speedGbps: 32 },
];

interface Product {
  partNumber: string;
  name: string;
  url: string;
  price?: number;
  formFactor: string;
  speed: string;
  speedGbps: number;
  reachLabel?: string;
  reachMeters?: number;
  fiberType?: string;
  wavelength?: string;
}

function sleep(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

function detectReach(text: string): { label: string; meters: number } | undefined {
  const patterns: [RegExp, string, number][] = [
    [/\b120\s*km\b/i, "120km", 120000],
    [/\b80\s*km\b/i, "80km", 80000],
    [/\b40\s*km\b/i, "40km", 40000],
    [/\b20\s*km\b/i, "20km", 20000],
    [/\b10\s*km\b/i, "10km", 10000],
    [/\b2\s*km\b/i, "2km", 2000],
    [/\b550\s*m\b/i, "550m", 550],
    [/\b500\s*m\b/i, "500m", 500],
    [/\b400\s*m\b/i, "400m", 400],
    [/\b300\s*m\b/i, "300m", 300],
    [/\b150\s*m\b/i, "150m", 150],
    [/\b100\s*m\b/i, "100m", 100],
    [/\b30\s*m\b/i, "30m", 30],
    [/\bLR4\b/, "10km", 10000],
    [/\bLR\b/, "10km", 10000],
    [/\bER4?\b/, "40km", 40000],
    [/\bZR4?\b/, "80km", 80000],
    [/\bSR4?\b/, "300m", 300],
    [/\bDR4?\b/, "500m", 500],
    [/\bFR4?\b/, "2km", 2000],
  ];
  for (const [regex, label, meters] of patterns) {
    if (regex.test(text)) return { label, meters };
  }
  return undefined;
}

function detectFiber(text: string): string {
  if (/single.?mode|smf|[^a-z]lx[^a-z]|[^a-z]lr[^a-z]|[^a-z]er[^a-z]|[^a-z]zr[^a-z]|bidi|cwdm|dwdm/i.test(text)) return "SMF";
  if (/multi.?mode|mmf|[^a-z]sx[^a-z]|[^a-z]sr[^a-z]/i.test(text)) return "MMF";
  if (/copper|dac|twinax|rj.?45|base-t|cat[56x]/i.test(text)) return "Copper";
  return "";
}

function detectWavelength(text: string): string {
  const match = text.match(/(\d{3,4})\s*nm/i);
  if (match) return match[1];
  return "";
}

function parseProductList(html: string, cat: typeof CATEGORIES[number]): Product[] {
  const products: Product[] = [];

  // Magento product listing: <h2 class="product-name"><a href="URL" title="NAME">NAME</a></h2>
  // Prices: <span class="price">US$XX.XX</span>
  const productRegex = /<h2\s+class="product-name">\s*<a\s+href="([^"]+)"[^>]*title="([^"]+)"[^>]*>/gi;
  let match;
  while ((match = productRegex.exec(html)) !== null) {
    const url = match[1];
    const name = match[2].trim();
    if (name.length < 5) continue;

    // Find price after this product name (within next 800 chars)
    const afterContext = html.slice(match.index, match.index + 800);
    const priceMatch = afterContext.match(/class="price">\s*US?\$\s*([\d,.]+)/);
    const price = priceMatch ? parseFloat(priceMatch[1].replace(",", "")) : undefined;

    const reach = detectReach(name);
    // Build part number from URL slug
    const slug = url.split("/").filter(Boolean).pop() || "";
    const partNumber = slug.replace(/-\d+$/, "").slice(0, 80);

    products.push({
      partNumber,
      name,
      url,
      price: price && price > 0 && price < 50000 ? price : undefined,
      formFactor: cat.formFactor,
      speed: cat.speed,
      speedGbps: cat.speedGbps,
      reachLabel: reach?.label,
      reachMeters: reach?.meters,
      fiberType: detectFiber(name),
      wavelength: detectWavelength(name),
    });
  }

  // Dedupe by URL
  const seen = new Set<string>();
  return products.filter((p) => {
    if (seen.has(p.url)) return false;
    seen.add(p.url);
    return true;
  });
}

async function fetchPage(url: string): Promise<string> {
  const resp = await fetch(url, {
    headers: HEADERS,
    signal: AbortSignal.timeout(30000),
  });
  if (!resp.ok) throw new Error(`HTTP ${resp.status} for ${url}`);
  return resp.text();
}

export async function scrapeSfpCables(): Promise<void> {
  console.log("=== SFPcables.com Scraper Starting ===\n");

  const vendorId = await ensureVendor("SFPcables", "compatible", "https://www.sfpcables.com", "https://www.sfpcables.com/transceivers");

  let totalProducts = 0;
  let priceUpdates = 0;

  for (const cat of CATEGORIES) {
    console.log(`\n--- ${cat.formFactor} (${cat.speed}) [${cat.path}] ---`);

    try {
      const html = await fetchPage(BASE + cat.path);
      const catProducts = parseProductList(html, cat);
      console.log(`  Found ${catProducts.length} products`);

      // Check for pagination (Magento uses ?p=N)
      const pageLinks = html.match(/[?&]p=(\d+)/g);
      let maxPage = 1;
      if (pageLinks) {
        for (const pl of pageLinks) {
          const n = parseInt(pl.replace(/[^0-9]/g, ""));
          if (n > maxPage) maxPage = n;
        }
      }

      // Fetch additional pages
      for (let page = 2; page <= Math.min(maxPage, 10); page++) {
        await sleep(2000);
        try {
          const pageHtml = await fetchPage(`${BASE}${cat.path}?p=${page}`);
          const pageProducts = parseProductList(pageHtml, cat);
          catProducts.push(...pageProducts);
          console.log(`  Page ${page}: ${pageProducts.length} products`);
        } catch (err) {
          console.warn(`  Page ${page} failed: ${(err as Error).message}`);
        }
      }

      for (const product of catProducts) {
        try {
          const txId = await findOrCreateScrapedTransceiver({
            partNumber: product.partNumber,
            vendorId,
            formFactor: product.formFactor,
            speedGbps: product.speedGbps,
            speed: product.speed,
            reachMeters: product.reachMeters,
            reachLabel: product.reachLabel,
            fiberType: product.fiberType,
            wavelengths: product.wavelength,
            category: "DataCenter",
          });

          if (product.price && product.price > 0) {
            const hash = contentHash({ price: product.price, part: product.partNumber });
            const updated = await upsertPriceObservation({
              transceiverId: txId,
              sourceVendorId: vendorId,
              price: product.price,
              currency: "USD",
              stockLevel: "in_stock",
              url: product.url,
              contentHash: hash,
            });
            if (updated) priceUpdates++;
          }
          totalProducts++;
        } catch (err) {
          console.warn(`    Error: ${(err as Error).message.slice(0, 80)}`);
        }
      }
    } catch (err) {
      console.error(`  Category failed: ${(err as Error).message}`);
    }

    await sleep(2000);
  }

  console.log(`\n=== SFPcables Complete: ${totalProducts} products, ${priceUpdates} prices ===`);
}

if (require.main === module) {
  scrapeSfpCables()
    .then(() => pool.end())
    .catch((err) => { console.error("Fatal:", err); pool.end(); process.exit(1); });
}
