from __future__ import annotations

import sys
from pathlib import Path

from pypdf import PdfReader


def main() -> None:
    if len(sys.argv) != 3:
        raise SystemExit("usage: extract_pdf_text.py <pdf> <output_txt>")
    pdf_path = Path(sys.argv[1])
    output_path = Path(sys.argv[2])
    reader = PdfReader(str(pdf_path))
    chunks: list[str] = []
    for index, page in enumerate(reader.pages, 1):
        text = page.extract_text() or ""
        chunks.append(f"\n\n===== PAGE {index} =====\n{text}")
    output_path.parent.mkdir(parents=True, exist_ok=True)
    output_path.write_text("".join(chunks), encoding="utf-8")
    print(f"pages={len(reader.pages)} output={output_path}")


if __name__ == "__main__":
    main()
