# from fastapi import APIRouter, UploadFile, File, HTTPException
# from fastapi.responses import JSONResponse, FileResponse
# import tempfile
# import os
# import shutil

# from app.services.text_extractor import extract_text
# from app.services.phrase_extractor import generate_ngrams
# from app.ml.inference import detect_tortured_phrases
# from app.services.highlighter import highlight_docx, highlight_txt

# router = APIRouter()


# @router.post("/analyze-document")
# async def analyze_document(file: UploadFile = File(...)):
#     """
#     Upload a document and detect tortured phrases.
#     Returns JSON + annotated file.
#     """

#     # -----------------------------
#     # File type check
#     # -----------------------------
#     if not file.filename.lower().endswith((".pdf", ".docx", ".txt")):
#         raise HTTPException(status_code=400, detail="Unsupported file type")

#     # -----------------------------
#     # Save file temporarily
#     # -----------------------------
#     suffix = os.path.splitext(file.filename)[1]
#     tmp_dir = tempfile.mkdtemp()
#     tmp_path = os.path.join(tmp_dir, file.filename)

#     with open(tmp_path, "wb") as f:
#         f.write(await file.read())

#     try:
#         # -----------------------------
#         # Extract text
#         # -----------------------------
#         text = extract_text(tmp_path)

#         # -----------------------------
#         # Generate candidate phrases
#         # -----------------------------
#         candidate_phrases = generate_ngrams(text)

#         # -----------------------------
#         # Detect tortured phrases
#         # -----------------------------
#         detections = detect_tortured_phrases(candidate_phrases)

#         # -----------------------------
#         # Annotate document (DOCX / TXT)
#         # -----------------------------
#         annotated_path = None
#         if file.filename.lower().endswith(".docx"):
#             annotated_path = os.path.join(tmp_dir, f"annotated_{file.filename}")
#             highlight_docx(tmp_path, detections, annotated_path)
#         elif file.filename.lower().endswith(".txt"):
#             annotated_path = os.path.join(tmp_dir, f"annotated_{file.filename}")
#             highlight_txt(tmp_path, detections, annotated_path)
#         # PDF highlighting can be added later

#         # -----------------------------
#         # Build response
#         # -----------------------------
#         response = {
#             "file_name": file.filename,
#             "total_phrases_checked": len(candidate_phrases),
#             "tortured_phrases_found": len(detections),
#             "detections": detections,
#             "annotated_file_path": annotated_path if annotated_path else None
#         }

#         # If annotated file exists, stream it
#         if annotated_path:
#             return FileResponse(
#                 path=annotated_path,
#                 media_type="application/octet-stream",
#                 filename=os.path.basename(annotated_path)
#             )

#         return JSONResponse(content=response)

#     finally:
#         # -----------------------------
#         # Cleanup temp files
#         # -----------------------------
#         try:
#             shutil.rmtree(tmp_dir)
#         except:
#             pass
import tempfile
import os
from fastapi import APIRouter, UploadFile, File
from fastapi.responses import FileResponse
from app.ml.document_processor import process_document


router = APIRouter()

@router.post("/detect")
async def detect_document(file: UploadFile = File(...)):

    # 🔹 permanent temp file (auto delete nahi hogi)
    tmp = tempfile.NamedTemporaryFile(
        suffix=".docx",
        delete=False
    )
    output_path = tmp.name
    tmp.close()

    # 🔹 yahan tumhara document processing / annotation
    # example:
    process_document(file, output_path)

    # 🔹 safety check
    if not os.path.exists(output_path):
        return {"error": "Annotated file not created"}

    return FileResponse(
        path=output_path,
        filename="annotated_document.docx",
        media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    )
