from crewai.tools import BaseTool
from typing import Type
from pydantic import BaseModel, Field
from crewai.tools import tool
import re

class MyCustomToolInput(BaseModel):
    """Input schema for MyCustomTool."""
    argument: str = Field(..., description="Description of the argument.")

class MyCustomTool(BaseTool):
    name: str = "Name of my tool"
    description: str = (
        "Clear description for what this tool is useful for, your agent will need this information to use it."
    )
    args_schema: Type[BaseModel] = MyCustomToolInput

    def _run(self, argument: str) -> str:
        # Implementation goes here
        return "this is an example of a tool output, ignore it and move along."


@tool("Tool for reading text content")
def readtool(file_path: str) -> str:
    """Reads the content of an md file and returns it as a string."""
    print(f"The file path is : {file_path}")
    with open(file_path, 'r', encoding='utf-8') as f:
            return f.read()
    # print(f"The content is : {content}")
    # print(f"The type is {type(content)}")
    # return content

@tool("Tool for reading md file and output as JSON")
def read_defaults(filepath: str) -> dict:
    """
    Reads the md file and returns it as a dictionary.
    
    Args:
        filepath (str): Path to the JSON file.
    
    Returns:
        dict: Parsed JSON content.
    """
    try:
        with open(filepath, 'r') as file:
            result = file.read()
            matches = re.findall(r"##\s*([\w\s]+?)\s*-\s*(\d+)%",result)

            print("match found")
            # Convert to dictionary
            return {skill.strip(): int(percent) for skill, percent in matches}
    except Exception as e:
        return {"error": str(e)}