/**
 * 10Gtek.com Scraper — Chinese OEM Transceiver Vendor
 *
 * 10Gtek's main site (www.10gtek.com) only shows technical spec tables, no prices.
 * Prices are available on their retail store: sfpcables.com (same company/brand).
 * This scraper targets sfpcables.com which has both part numbers and USD prices.
 *
 * Strategy: Paginate each category on sfpcables.com, extract Model + price per product.
 * Rate limited: 1 req/2sec between pages.
 *
 * Categories: SFP, SFP+, SFP28, QSFP+, QSFP28, XFP, QSFP-DD 400G, QSFP112 400G
 */
import { pool, findOrCreateScrapedTransceiver, ensureVendor, upsertPriceObservation } from "../utils/db";
import { contentHash, parsePrice } 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 = [
  { slug: "sfp-1-25g-series",                    formFactor: "SFP",     speed: "1G",   speedGbps: 1   },
  { slug: "sfp-transceivers",                     formFactor: "SFP+",    speed: "10G",  speedGbps: 10  },
  { slug: "sfp28-transceivers",                   formFactor: "SFP28",   speed: "25G",  speedGbps: 25  },
  { slug: "qsfp-transceivers",                    formFactor: "QSFP+",   speed: "40G",  speedGbps: 40  },
  { slug: "100g-qsfp28-transceivers",             formFactor: "QSFP28",  speed: "100G", speedGbps: 100 },
  { slug: "xfp-transceivers",                     formFactor: "XFP",     speed: "10G",  speedGbps: 10  },
  // 400G — added to close pricing gap for TIP_LLM training data
  { slug: "8x50g-qsfp-dd-transceiver-optical-module", formFactor: "QSFP-DD",  speed: "400G", speedGbps: 400 },
  { slug: "qsfp112-400g",                         formFactor: "QSFP112", speed: "400G", speedGbps: 400 },
  { slug: "400g-qsfp-fiber-optic-transceiver-modules", formFactor: "QSFP-DD",  speed: "400G", speedGbps: 400 },
];

interface Product {
  partNumber: string;
  name: string;
  url: string;
  price?: number;
  currency?: string;
  formFactor: string;
  speed: string;
  speedGbps: number;
  reachLabel?: string;
  reachMeters?: number;
  fiberType?: string;
  imageUrl?: 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][] = [
    [/\b1\s*-\s*4\s*km\b/i, "4km", 4000],
    [/\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],
    [/\b500\s*m\b/i, "500m", 500],
    [/\b300\s*m\b/i, "300m", 300],
    [/\b100\s*m\b/i, "100m", 100],
    [/\b80\s*meters?\b/i, "80m", 80],
    [/\b40\s*meters?\b/i, "40m", 40],
    [/\b30\s*meters?\b/i, "30m", 30],
    [/\bLR4\b/, "10km", 10000],
    [/\bLR\b/, "10km", 10000],
    [/\bER\b/, "40km", 40000],
    [/\bZR\b/, "80km", 80000],
    [/\bSR4?\b/, "100m", 100],
    [/\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|rj.?45/i.test(text)) return "Copper";
  return "";
}

/** Strip HTML tags and decode common entities */
function stripHtml(s: string): string {
  return s
    .replace(/<[^>]+>/g, "")
    .replace(/&amp;/g, "&")
    .replace(/&lt;/g, "<")
    .replace(/&gt;/g, ">")
    .replace(/&nbsp;/g, " ")
    .replace(/&#\d+;/g, "")
    .trim();
}

/**
 * Parse product listings from a sfpcables.com category page.
 *
 * HTML structure per product (Magento):
 *   <div class="product-shop">
 *     <h2 class="product-name"><a href="URL" title="NAME">NAME</a></h2>
 *     <div>Model: PART_NUMBER</div>          ← most categories
 *     <span class="price">US$X.XX</span>     ← appears twice; first = listing price
 *   </div>
 *
 * XFP exception: no Model: div; part number is in title after "|"
 *   e.g. "XFP Transceiver 10GBase-SR 850nm, 300M | XFP-10G-MM-SR"
 */
function parseProductsFromPage(html: string, cat: typeof CATEGORIES[number]): Product[] {
  const products: Product[] = [];

  // Each product block starts at product-shop, ends before next product-shop or toolbar-bottom
  const blockRegex =
    /<div class="product-shop">([\s\S]*?)(?=<div class="product-shop"|<div class="toolbar-bottom">)/g;
  let blockMatch: RegExpExecArray | null;

  while ((blockMatch = blockRegex.exec(html)) !== null) {
    const block = blockMatch[1];

    // Extract product URL and title from <h2 class="product-name"><a href="..." title="...">
    const nameMatch = block.match(/<h2 class="product-name">\s*<a[^>]+href="([^"]+)"[^>]+title="([^"]+)"/);
    if (!nameMatch) continue;
    const productUrl = nameMatch[1].trim();
    const rawTitle = stripHtml(nameMatch[2]);

    // Extract part number from <div>Model: PART</div>
    const modelMatch = block.match(/<div>Model:\s*([^<]+)<\/div>/i);
    let partNumber = modelMatch ? modelMatch[1].trim() : "";

    // XFP fallback: parse title after "|"
    if (!partNumber && rawTitle.includes("|")) {
      const afterPipe = rawTitle.split("|").pop()?.trim() ?? "";
      if (afterPipe.length >= 3 && /^[A-Z0-9]/i.test(afterPipe)) {
        partNumber = afterPipe.trim();
      }
    }

    // Skip products without a usable part number
    if (!partNumber || partNumber.length < 3) continue;

    // Extract price — first occurrence of US$X.XX in the block
    const priceMatch = block.match(/<span class="price">US\$([0-9]+(?:\.[0-9]{1,2})?)<\/span>/);
    const price = priceMatch ? parseFloat(priceMatch[1]) : undefined;

    // Detect reach and fiber type from product title
    const reach = detectReach(rawTitle);
    const fiber = detectFiber(rawTitle);
    const imageMatch = block.match(/<img[^>]+(?:src|data-src)="([^"]+)"/i);
    const imageUrl = imageMatch?.[1]
      ? (imageMatch[1].startsWith("http") ? imageMatch[1] : `${BASE}${imageMatch[1]}`)
      : undefined;

    products.push({
      partNumber,
      name: rawTitle,
      url: productUrl,
      price,
      currency: price !== undefined ? "USD" : undefined,
      formFactor: cat.formFactor,
      speed: cat.speed,
      speedGbps: cat.speedGbps,
      reachLabel: reach?.label,
      reachMeters: reach?.meters,
      fiberType: fiber || undefined,
      imageUrl,
    });
  }

  return products;
}

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();
}

/**
 * Fetch all pages of a category, stopping when no new part numbers appear.
 * Magento loops back to page 1 for out-of-range page numbers, so we detect
 * this by checking for duplicate part numbers from previous pages.
 */
async function fetchAllPages(cat: typeof CATEGORIES[number]): Promise<Product[]> {
  const MAX_PAGES = 30;
  const allProducts: Product[] = [];
  const seenPartNumbers = new Set<string>();

  for (let page = 1; page <= MAX_PAGES; page++) {
    const url = page === 1 ? `${BASE}/${cat.slug}` : `${BASE}/${cat.slug}?p=${page}`;

    let html: string;
    try {
      html = await fetchPage(url);
    } catch (err) {
      console.error(`    Fetch error on p${page}: ${(err as Error).message}`);
      break;
    }

    const pageProducts = parseProductsFromPage(html, cat);

    // Stop if no products found (truly empty page)
    if (pageProducts.length === 0) break;

    // Count new (unseen) products — detects Magento catalog wrap-around
    let newCount = 0;
    for (const p of pageProducts) {
      if (!seenPartNumbers.has(p.partNumber)) {
        seenPartNumbers.add(p.partNumber);
        allProducts.push(p);
        newCount++;
      }
    }

    console.log(`    p${page}: ${pageProducts.length} parsed, ${newCount} new`);

    // All products on this page already seen → we've looped back to start
    if (newCount === 0) break;

    await sleep(2000);
  }

  return allProducts;
}

export async function scrape10Gtek(): Promise<void> {
  console.log("=== 10Gtek Scraper Starting (via sfpcables.com) ===\n");

  const vendorId = await ensureVendor(
    "10Gtek",
    "compatible",
    "https://www.10gtek.com",
    "https://www.sfpcables.com"
  );

  let totalProducts = 0;
  let priceUpdates = 0;

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

    try {
      const catProducts = await fetchAllPages(cat);
      console.log(`  Fetched: ${catProducts.length} unique products`);

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

          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: product.currency ?? "USD",
              stockLevel: "in_stock",
              url: product.url,
              contentHash: hash,
            });
            if (updated) priceUpdates++;
          }

          totalProducts++;
        } catch (err) {
          console.warn(`    Error [${product.partNumber}]: ${(err as Error).message.slice(0, 80)}`);
        }
      }
    } catch (err) {
      console.error(`  Category failed: ${(err as Error).message}`);
    }
  }

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

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