"""
{{ description }}
"""
import time
import uuid
from typing import Callable

from fastapi import FastAPI, Request, Response
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.types import ASGIApp, Receive, Scope, Send
import logging

logger = logging.getLogger(__name__)


class {{ class_name }}Middleware(BaseHTTPMiddleware):
    """Middleware for {{ name }}"""
    
    def __init__(self, app: ASGIApp):
        """
        Initialize the middleware.
        
        Args:
            app: ASGI application
        """
        super().__init__(app)
    
    async def dispatch(self, request: Request, call_next: Callable) -> Response:
        """
        Process the request and response.
        
        Args:
            request: FastAPI request
            call_next: Next middleware or route handler
            
        Returns:
            Response
        """
        # Implement your middleware logic here
        # For example, you can add headers, log requests, etc.
        
        # Before request processing
        start_time = time.time()
        
        # Generate request ID
        request_id = str(uuid.uuid4())
        request.state.request_id = request_id
        
        # Call next middleware or route handler
        try:
            response = await call_next(request)
        except Exception as e:
            # Handle exceptions
            logger.error(f"Error processing request {request_id}: {str(e)}")
            raise
        
        # After request processing
        process_time = time.time() - start_time
        
        # Add custom headers
        response.headers["X-Request-ID"] = request_id
        response.headers["X-Process-Time"] = str(process_time)
        
        return response


def add_{{ name }}_middleware(app: FastAPI) -> None:
    """
    Add {{ name }} middleware to the FastAPI application.
    
    Args:
        app: FastAPI application
    """
    app.add_middleware({{ class_name }}Middleware)