from collections.abc import Generator
from pathlib import Path
from tempfile import NamedTemporaryFile

from dify_plugin import Tool
from dify_plugin.entities.tool import ToolInvokeMessage

from scripts.services.svc_md_to_html import convert_md_to_html
from scripts.utils.file_utils import get_meta_data
from scripts.utils.logger_utils import get_logger
from scripts.utils.mimetype_utils import MimeType
from scripts.utils.param_utils import get_md_text_from_tool_params


class MarkdownToHtmlTool(Tool):
    logger = get_logger(__name__)

    def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]:
        """
        invoke tools
        """
        # get parameters
        md_text = get_md_text_from_tool_params(tool_parameters)

        try:
            # Create temporary output file
            with NamedTemporaryFile(suffix=".html", delete=False) as temp_output_file:
                temp_output_path = Path(temp_output_file.name)

            try:
                # Convert to HTML using the public service
                convert_md_to_html(md_text, temp_output_path, is_strip_wrapper=True)

                # Read the converted file content
                result_file_bytes = temp_output_path.read_bytes()

                yield self.create_blob_message(
                    blob=result_file_bytes,
                    meta=get_meta_data(
                        mime_type=MimeType.HTML,
                        output_filename=tool_parameters.get("output_filename"),
                    ),
                )
            finally:
                # Clean up temporary output file
                if temp_output_path.exists():
                    temp_output_path.unlink()
        except Exception as e:
            self.logger.exception("Failed to convert file")
            yield self.create_text_message(f"Failed to convert markdown text to HTML file, error: {str(e)}")
            raise e
