# -*- coding: utf-8 -*-
"""Stdio transport implementation for MCP server"""
import logging
import os
from pathlib import Path

from .base import BaseTransport

logger = logging.getLogger(__name__)


class StdioTransport(BaseTransport):
    """Stdio transport for MCP server communication"""

    def __init__(self, app):
        """
        Initialize stdio transport

        Args:
            app: The FastMCP application instance
        """
        self.app = app

    def run(self) -> None:
        """Start the MCP server with stdio transport and block until complete"""
        logging.basicConfig(level=logging.INFO)

        # Create storage directory if IMAGE_STORAGE_DIR is configured
        storage_dir = os.environ.get("IMAGE_STORAGE_DIR")
        if storage_dir:
            Path(storage_dir).mkdir(parents=True, exist_ok=True)

        # Run the MCP server with stdio transport
        self.app.run(transport="stdio")


def run_stdio(app) -> None:
    """
    Convenience function to run stdio transport

    Args:
        app: The FastMCP application instance
    """
    transport = StdioTransport(app)
    transport.run()
