from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

from .converter import convert_pdf


def configure_stdout_utf8() -> None:
    if hasattr(sys.stdout, "reconfigure"):
        sys.stdout.reconfigure(encoding="utf-8")


def main() -> int:
    configure_stdout_utf8()
    parser = argparse.ArgumentParser()
    parser.add_argument("--input", required=True)
    parser.add_argument("--mode", default="auto", choices=["auto", "digital_text", "local_ocr"])
    parser.add_argument("--output-dir", required=True)
    args = parser.parse_args()
    result = convert_pdf(Path(args.input), Path(args.output_dir), args.mode)
    print(json.dumps(result.to_dict(), ensure_ascii=False, indent=2))
    return 0 if result.status == "ok" else 2


if __name__ == "__main__":
    raise SystemExit(main())
