import re
from pathlib import Path
from typing import Union

import pdfplumber


def count_words(pdf_path: Union[str, Path]) -> int:
    """Count words in a PDF file and return the total count."""
    path = Path(pdf_path)

    if not path.exists():
        raise FileNotFoundError(f"File not found: {pdf_path}")

    with pdfplumber.open(path) as pdf:
        full_text = []
        for page in pdf.pages:
            text = page.extract_text()
            if text:
                full_text.append(text)

    words = re.findall(r"\S+", " ".join(full_text))
    return len(words)
