import argparse
import json
from pathlib import Path

from cv_analysis.server.pipeline import get_analysis_pipeline


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("pdf")
    parser.add_argument("--type", "-t", choices=["table", "layout", "figure"], required=True)
    return parser.parse_args()


if __name__ == "__main__":
    args = parse_args()

    analysis_fn = get_analysis_pipeline(args.type)

    with open(args.pdf, "rb") as f:
        pdf_bytes = f.read()

    results = list(analysis_fn(pdf_bytes))

    folder = Path(args.pdf).parent
    file_stem = Path(args.pdf).stem

    with open(f"{folder}/{file_stem}_{args.type}.json", "w+") as f:
        json.dump(results, f, indent=2)
