# Copyright (c) ModelScope Contributors. All rights reserved.
"""
Tools API endpoints for Sirchmunk
Handles quick actions like file export, PPT generation, document conversion, etc.
"""

from fastapi import APIRouter, HTTPException
from typing import Dict, List, Any, Optional
import uuid
from datetime import datetime, timedelta
import random

router = APIRouter(prefix="/api/v1/tools", tags=["tools"])

# Mock tool configurations
TOOL_CONFIGS = {
    "export-pdf": {
        "name": "PDF Export",
        "description": "Export conversation or content to PDF format",
        "processing_time": (2, 5),  # seconds range
        "output_format": "pdf"
    },
    "generate-ppt": {
        "name": "PPT Generation", 
        "description": "Generate PowerPoint presentation from content",
        "processing_time": (5, 10),
        "output_format": "pptx"
    },
    "convert-doc": {
        "name": "Document Conversion",
        "description": "Convert documents between different formats",
        "processing_time": (3, 7),
        "output_format": "various"
    },
    "generate-video": {
        "name": "Video Generation",
        "description": "Create video content from text or images",
        "processing_time": (15, 30),
        "output_format": "mp4"
    },
    "create-image": {
        "name": "Image Creation",
        "description": "Generate images using AI",
        "processing_time": (3, 8),
        "output_format": "png"
    },
    "export-excel": {
        "name": "Excel Export",
        "description": "Export data to Excel spreadsheet",
        "processing_time": (2, 4),
        "output_format": "xlsx"
    }
}

@router.get("/")
async def list_available_tools():
    """List all available tools and their configurations"""
    try:
        tools = []
        for tool_id, config in TOOL_CONFIGS.items():
            tools.append({
                "id": tool_id,
                "name": config["name"],
                "description": config["description"],
                "output_format": config["output_format"],
                "estimated_time": f"{config['processing_time'][0]}-{config['processing_time'][1]}s"
            })
        
        return {
            "success": True,
            "data": tools,
            "timestamp": datetime.now().isoformat()
        }
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to list tools: {str(e)}")

@router.post("/{tool_id}")
async def execute_tool(tool_id: str, request: Dict[str, Any]):
    """
    Execute a specific tool
    
    Args:
        tool_id: ID of the tool to execute
        request: Tool execution parameters
    """
    try:
        if tool_id not in TOOL_CONFIGS:
            raise HTTPException(status_code=404, detail=f"Tool '{tool_id}' not found")
        
        config = TOOL_CONFIGS[tool_id]
        
        # Simulate processing time
        processing_time = random.randint(*config["processing_time"])
        
        # Generate mock result based on tool type
        result = await _generate_tool_result(tool_id, config, request)
        
        return {
            "success": True,
            "data": result,
            "message": f"{config['name']} completed successfully",
            "timestamp": datetime.now().isoformat()
        }
        
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to execute tool: {str(e)}")

async def _generate_tool_result(tool_id: str, config: Dict[str, Any], request: Dict[str, Any]) -> Dict[str, Any]:
    """Generate mock result for tool execution"""
    
    base_result = {
        "task_id": str(uuid.uuid4()),
        "tool_id": tool_id,
        "tool_name": config["name"],
        "started_at": datetime.now().isoformat(),
        "completed_at": (datetime.now() + timedelta(seconds=random.randint(*config["processing_time"]))).isoformat(),
        "status": "completed"
    }
    
    if tool_id == "export-pdf":
        return {
            **base_result,
            "output": {
                "file_name": f"conversation_export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf",
                "file_size": f"{random.randint(500, 2000)} KB",
                "pages": random.randint(3, 15),
                "download_url": f"https://example.com/downloads/pdf_{base_result['task_id']}.pdf"
            },
            "metadata": {
                "format": "PDF",
                "quality": "high",
                "includes_images": True,
                "includes_formatting": True
            }
        }
    
    elif tool_id == "generate-ppt":
        return {
            **base_result,
            "output": {
                "file_name": f"presentation_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pptx",
                "file_size": f"{random.randint(2, 8)} MB",
                "slides": random.randint(8, 20),
                "download_url": f"https://example.com/downloads/ppt_{base_result['task_id']}.pptx"
            },
            "metadata": {
                "template": "professional",
                "theme": "modern",
                "includes_charts": True,
                "includes_animations": False
            }
        }
    
    elif tool_id == "convert-doc":
        source_format = request.get("source_format", "docx")
        target_format = request.get("target_format", "pdf")
        
        return {
            **base_result,
            "output": {
                "file_name": f"converted_document_{datetime.now().strftime('%Y%m%d_%H%M%S')}.{target_format}",
                "file_size": f"{random.randint(800, 3000)} KB",
                "download_url": f"https://example.com/downloads/converted_{base_result['task_id']}.{target_format}"
            },
            "metadata": {
                "source_format": source_format,
                "target_format": target_format,
                "conversion_quality": "high",
                "preserved_formatting": True
            }
        }
    
    elif tool_id == "generate-video":
        return {
            **base_result,
            "output": {
                "file_name": f"generated_video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4",
                "file_size": f"{random.randint(10, 50)} MB",
                "duration": f"{random.randint(30, 180)} seconds",
                "download_url": f"https://example.com/downloads/video_{base_result['task_id']}.mp4"
            },
            "metadata": {
                "resolution": "1920x1080",
                "fps": 30,
                "codec": "H.264",
                "audio": True
            }
        }
    
    elif tool_id == "create-image":
        return {
            **base_result,
            "output": {
                "file_name": f"generated_image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png",
                "file_size": f"{random.randint(500, 2000)} KB",
                "dimensions": "1024x1024",
                "download_url": f"https://example.com/downloads/image_{base_result['task_id']}.png"
            },
            "metadata": {
                "format": "PNG",
                "quality": "high",
                "style": "photorealistic",
                "ai_model": "DALL-E 3"
            }
        }
    
    elif tool_id == "export-excel":
        return {
            **base_result,
            "output": {
                "file_name": f"data_export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx",
                "file_size": f"{random.randint(200, 1500)} KB",
                "sheets": random.randint(1, 5),
                "rows": random.randint(100, 5000),
                "download_url": f"https://example.com/downloads/excel_{base_result['task_id']}.xlsx"
            },
            "metadata": {
                "format": "Excel 2019",
                "includes_charts": True,
                "includes_formulas": True,
                "data_types": ["text", "numbers", "dates"]
            }
        }
    
    else:
        return {
            **base_result,
            "output": {
                "message": "Tool executed successfully",
                "result": "Generic tool result"
            }
        }

@router.get("/{tool_id}/status")
async def get_tool_status(tool_id: str):
    """Get the current status of a tool"""
    try:
        if tool_id not in TOOL_CONFIGS:
            raise HTTPException(status_code=404, detail=f"Tool '{tool_id}' not found")
        
        config = TOOL_CONFIGS[tool_id]
        
        status_data = {
            "tool_id": tool_id,
            "name": config["name"],
            "status": "available",
            "description": config["description"],
            "output_format": config["output_format"],
            "estimated_processing_time": f"{config['processing_time'][0]}-{config['processing_time'][1]} seconds",
            "usage_stats": {
                "total_executions": random.randint(50, 500),
                "success_rate": round(random.uniform(0.85, 0.99), 2),
                "average_processing_time": f"{random.randint(config['processing_time'][0], config['processing_time'][1])} seconds"
            },
            "last_used": (datetime.now() - timedelta(hours=random.randint(1, 48))).isoformat()
        }
        
        return {
            "success": True,
            "data": status_data,
            "timestamp": datetime.now().isoformat()
        }
        
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to get tool status: {str(e)}")

@router.get("/history")
async def get_tool_execution_history(
    tool_id: Optional[str] = None,
    limit: int = 20,
    offset: int = 0
):
    """Get tool execution history"""
    try:
        # Generate mock history
        history = []
        
        for i in range(limit):
            if tool_id and tool_id not in TOOL_CONFIGS:
                continue
                
            selected_tool_id = tool_id or random.choice(list(TOOL_CONFIGS.keys()))
            config = TOOL_CONFIGS[selected_tool_id]
            
            execution = {
                "id": str(uuid.uuid4()),
                "tool_id": selected_tool_id,
                "tool_name": config["name"],
                "status": random.choice(["completed", "completed", "completed", "failed"]),
                "started_at": (datetime.now() - timedelta(hours=random.randint(1, 168))).isoformat(),
                "completed_at": (datetime.now() - timedelta(hours=random.randint(1, 168))).isoformat(),
                "processing_time": f"{random.randint(*config['processing_time'])} seconds",
                "output_size": f"{random.randint(100, 5000)} KB"
            }
            
            history.append(execution)
        
        return {
            "success": True,
            "data": history,
            "pagination": {
                "limit": limit,
                "offset": offset,
                "total": random.randint(100, 1000)
            },
            "timestamp": datetime.now().isoformat()
        }
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to get tool history: {str(e)}")