#!/usr/bin/env python3
"""Tiny capability check for a MarkItDown installation: converts one small sample per format and prints a yes/no table.
Usage: python check_installation.py [folder-with-samples]   (the samples are the files published at /samples/ on the site)
"""
import sys, pathlib, contextlib, io
from markitdown import MarkItDown, MissingDependencyException, FileConversionException, UnsupportedFormatException

FILES = [("PDF", "pdf-text.pdf"), ("Word .docx", "report.docx"), ("PowerPoint .pptx", "deck.pptx"), ("Excel .xlsx", "workbook.xlsx"),
         ("HTML", "page.html"), ("CSV", "data.csv"), ("EPUB", "book.epub"), ("ZIP", "bundle.zip"), ("Jupyter notebook", "notebook.ipynb"), ("Word .doc (old)", "report.doc"), ("GIF image", "unsupported.gif")]
folder = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else "samples")
md = MarkItDown()
print(f"{'format':<18}{'result':<10}note")
for label, name in FILES:
    path = folder / name
    try:
        with contextlib.redirect_stderr(io.StringIO()):
            text = md.convert(str(path)).markdown
        print(f"{label:<18}{'yes':<10}{len(text)} characters")
    except FileConversionException as e:
        inner = e.attempts[0].exc_info[1] if e.attempts else None
        if isinstance(inner, MissingDependencyException):
            print(f"{label:<18}{'no':<10}needs an extra: {type(e.attempts[0].converter).__name__}")
        else:
            print(f"{label:<18}{'failed':<10}{type(inner).__name__ if inner else 'unknown'}")
    except UnsupportedFormatException:
        print(f"{label:<18}{'no':<10}UnsupportedFormatException")
