from docx import Document
from docx.shared import RGBColor
from PyPDF2 import PdfReader, PdfWriter
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
import io
import os

def highlight_docx(input_path: str, detections: list, output_path: str):
    """
    Highlight phrases in DOCX with red color
    """
    doc = Document(input_path)
    for para in doc.paragraphs:
        for det in detections:
            if det["phrase"] in para.text:
                inline = para.runs
                for i in range(len(inline)):
                    if det["phrase"] in inline[i].text:
                        start = inline[i].text.find(det["phrase"])
                        end = start + len(det["phrase"])
                        inline[i].font.color.rgb = RGBColor(255, 0, 0)
    doc.save(output_path)
    return output_path

def highlight_txt(input_path: str, detections: list, output_path: str):
    """
    Highlight phrases in TXT by adding markers [HIGHLIGHT]
    """
    with open(input_path, "r", encoding="utf-8") as f:
        text = f.read()
    for det in detections:
        text = text.replace(det["phrase"], f"[HIGHLIGHT]{det['phrase']}[/HIGHLIGHT]")
    with open(output_path, "w", encoding="utf-8") as f:
        f.write(text)
    return output_path
