import os
import sys
import win32com.client

def convert_to_pdf(doc_paths):
    word = win32com.client.Dispatch("Word.Application")
    word.Visible = False
    
    # wdExportFormatPDF = 17
    # wdExportOptimizeForPrint = 0
    # wdExportCreateWordBookmarks = 1 (wdExportCreateHeadingBookmarks)
    
    try:
        for doc_path in doc_paths:
            print(f"Converting: {doc_path}")
            if not os.path.exists(doc_path):
                print(f"Error: File not found: {doc_path}")
                continue
                
            abs_path = os.path.abspath(doc_path)
            pdf_path = os.path.splitext(abs_path)[0] + ".pdf"
            
            try:
                doc = word.Documents.Open(abs_path)
                
                # Force comments to be visible
                word.ActiveWindow.View.ShowComments = True
                word.ActiveWindow.View.RevisionsMode = 0
                
                # Global options to ensure comments are printed/exported
                word.Options.PrintComments = True
                word.Options.PrintHiddenText = True 
                
                doc.ExportAsFixedFormat(
                    OutputFileName=pdf_path,
                    ExportFormat=17, # wdExportFormatPDF
                    OpenAfterExport=False,
                    OptimizeFor=1, # wdExportOptimizeForOnScreen
                    Item=7, # wdExportDocumentWithMarkup
                    IncludeDocProps=True,
                    KeepIRM=True,
                    CreateBookmarks=0, # No bookmarks
                    DocStructureTags=True,
                    BitmapMissingFonts=True,
                    UseISO19005_1=False # PDF/A
                )
                
                doc.Close(SaveChanges=False)
                print(f"Success: {pdf_path}")
            except Exception as e:
                print(f"Failed to convert {doc_path}: {e}")
                
    finally:
        word.Quit()

if __name__ == "__main__":
    files = [
        r"C:\Users\Administrator\.gemini\antigravity\scratch\bluebeam-comment-conv\05 05 23.13 10 markup.docx",
        r"C:\Users\Administrator\.gemini\antigravity\scratch\bluebeam-comment-conv\05 12 00 markup.docx",
        r"C:\Users\Administrator\.gemini\antigravity\scratch\bluebeam-comment-conv\05 21 00 markup.docx",
        r"C:\Users\Administrator\.gemini\antigravity\scratch\bluebeam-comment-conv\05 30 00 markup.docx",
        r"C:\Users\Administrator\.gemini\antigravity\scratch\bluebeam-comment-conv\05 40 00 markup.docx",
        r"C:\Users\Administrator\.gemini\antigravity\scratch\bluebeam-comment-conv\05 50 14 markup.docx",
        r"C:\Users\Administrator\.gemini\antigravity\scratch\bluebeam-comment-conv\06 17 19 markup.docx"
    ]
    convert_to_pdf(files)
