"""
Extract text from PDF into paragraph chunks.
Saves to data/processed/permenpar_paragraphs.txt (one paragraph per line).
"""
import pdfplumber, re
from pathlib import Path

PDF = Path("./data/raw/Permenpar_6_Tahun_2025.pdf")
OUT = Path("./data/processed/permenpar_paragraphs.txt")

def normalize(text):
    text = text.replace("\r","")
    text = re.sub(r"\n{2,}", "\n\n", text)
    text = re.sub(r"\s{2,}", " ", text)
    return text.strip()

with pdfplumber.open(PDF) as pdf:
    pages = [p.extract_text() or "" for p in pdf.pages]

full = "\\n".join(pages)
full = normalize(full)

# split into paragraphs by double newline or by "NO. KBLI" blocks
parts = []
if "NO. KBLI" in full:
    blocks = full.split("\\nNO. KBLI")
    for b in blocks:
        b = b.strip()
        if not b:
            continue
        if b.startswith("("):
            parts.append("NO. KBLI " + b)
        else:
            parts.append(b)
else:
    for p in full.split("\\n\\n"):
        p = p.strip()
        if len(p) > 30:
            parts.append(p)

OUT.parent.mkdir(parents=True, exist_ok=True)
with open(OUT, "w", encoding="utf-8") as f:
    for p in parts:
        f.write(p.replace("\\n"," ") + "\\n")
print("Saved", len(parts), "paragraphs to", OUT)
