# Copyright (c) 2025 Patrick Motsch
# All rights reserved.

import logging
from typing import Dict, Any
from modules.datamodels.datamodelChat import ActionResult

logger = logging.getLogger(__name__)

async def summarizeDocument(self, parameters: Dict[str, Any]) -> ActionResult:
    documentList = parameters.get("documentList", [])
    if not documentList:
        return ActionResult.isFailure(error="documentList is required")
    
    summaryLength = parameters.get("summaryLength", "medium")
    focus = parameters.get("focus")
    resultType = parameters.get("resultType", "txt")
    
    lengthInstructions = {
        "brief": "Create a brief summary (2-3 paragraphs)",
        "medium": "Create a medium-length summary (comprehensive but concise)",
        "detailed": "Create a detailed summary covering all major points"
    }
    lengthInstruction = lengthInstructions.get(summaryLength.lower(), lengthInstructions["medium"])
    
    aiPrompt = f"Summarize the provided document(s). {lengthInstruction}."
    if focus:
        aiPrompt += f" Focus specifically on: {focus}."
    aiPrompt += " Extract and present the key points, main ideas, and important information in a clear, well-structured format."
    
    # Pass parentOperationId to maintain progress hierarchy
    parentOperationId = parameters.get("parentOperationId")
    
    processParams = {
        "aiPrompt": aiPrompt,
        "documentList": documentList,
        "resultType": resultType,
        "generationIntent": "document"  # NEW: Explicit intent
    }
    if parentOperationId:
        processParams["parentOperationId"] = parentOperationId
    
    return await self.process(processParams)

