import { readFileSync } from "node:fs";

const acc = process.argv[2] ?? "0001493152-24-018935"; // MARIZYME default
const html = readFileSync(`../data/raw/html/${acc}.html`, "utf-8");

const stripped = html
  .replace(/<script[\s\S]*?<\/script>/gi, "")
  .replace(/<style[\s\S]*?<\/style>/gi, "")
  .replace(/<noscript[\s\S]*?<\/noscript>/gi, "")
  .replace(/<\/(span|a|b|i|u|em|strong|font)>\s*<(?:span|a|b|i|u|em|strong|font)[^>]*>/gi, "")
  .replace(/<\/ix:[a-z]+>\s*<ix:[a-z]+[^>]*>/gi, "")
  .replace(/<\/(p|div|tr|li|h[1-6]|td|th)>/gi, "\n")
  .replace(/<(br|hr)\s*\/?>/gi, "\n")
  .replace(/<[^>]+>/g, " ")
  .replace(/&nbsp;|&#160;|&#xa0;/gi, " ")
  .replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">")
  .replace(/&quot;/g, '"').replace(/&#39;|&apos;/g, "'")
  .replace(/&mdash;|&#8212;/g, "—").replace(/&ndash;|&#8211;/g, "–")
  .replace(/&bull;|&#8226;/g, "•")
  .replace(/&#\d+;/g, " ").replace(/&\w+;/g, " ")
  .replace(/[^\S\n]+/g, " ");

const rawLines = stripped.split("\n").map(l => l.trim()).filter(l => l.length > 0);
// Rejoin broken headings: "ITEM\n2. PROPERTIES" → "ITEM 2. PROPERTIES"
const lines: string[] = [];
for (let i = 0; i < rawLines.length; i++) {
  const line = rawLines[i]!;
  if (/^\s*(item|ITEM|Item)\s*$/i.test(line) && i + 1 < rawLines.length && /^\s*\d/.test(rawLines[i + 1]!)) {
    lines.push(line + " " + rawLines[i + 1]!);
    i++;
  } else {
    lines.push(line);
  }
}
console.log(`Total lines: ${lines.length}`);

const ITEM_1C = /^\s*(\u2022\s*)?item\s*1c[\.\s\u00a0—–:-]/i;
const NEXT_ITEM = /^\s*item\s*(\d+[a-z]?\.?\d*)/i;
const SECTION_END = /^\s*(signatures?|part\s*(ii|2|iii|3|iv|4)|exhibit\s*index|financial\s+statements)/i;
const POST_1C = /^\s*(properties|legal\s+proceedings|mine\s+safety|market\s+for\s+registrant|selected\s+financial|management.s\s+discussion|equity\s+compensation\s+plan|stock\s+performance)/i;

let startIdx = -1;
for (let i = 0; i < lines.length; i++) {
  if (ITEM_1C.test(lines[i]!) && lines[i]!.length < 300) {
    startIdx = i;
    console.log(`\nItem 1C heading at line ${i}: "${lines[i]!.slice(0, 120)}"`);
  }
}

if (startIdx === -1) {
  console.log("No Item 1C heading found!");
  // Check if it exists anywhere
  for (let i = 0; i < lines.length; i++) {
    if (/item\s*1c/i.test(lines[i]!)) {
      console.log(`  item 1c (non-heading) at line ${i}: "${lines[i]!.slice(0, 120)}"`);
    }
  }
  process.exit(0);
}

// Show lines after Item 1C to find where section ends
console.log(`\nLines after Item 1C (${startIdx}):`);
for (let i = startIdx + 1; i < Math.min(startIdx + 40, lines.length); i++) {
  const isEnd = SECTION_END.test(lines[i]!);
  const isPost = POST_1C.test(lines[i]!) && lines[i]!.length < 150;
  const itemMatch = NEXT_ITEM.exec(lines[i]!);
  const marker = isEnd ? " [SECTION_END]" : isPost ? " [POST_1C]" : itemMatch ? ` [ITEM ${itemMatch[1]}]` : "";
  if (marker || i <= startIdx + 5 || i >= startIdx + 35) {
    console.log(`  [${i}]${marker} "${lines[i]!.slice(0, 120)}"`);
  }
}
