"""
Middleware for tracking API response times and error rates.
"""
import time
import logging
from django.core.cache import cache
from django.utils.deprecation import MiddlewareMixin

logger = logging.getLogger(__name__)


class MetricsMiddleware(MiddlewareMixin):
    """
    Middleware to track API response times and error rates.
    Stores metrics in cache for periodic collection.
    """
    
    def process_request(self, request):
        """Record request start time."""
        request._metrics_start_time = time.time()
        return None
    
    def process_response(self, request, response):
        """Record response time and update metrics."""
        if hasattr(request, '_metrics_start_time'):
            response_time = (time.time() - request._metrics_start_time) * 1000  # Convert to ms
            
            # Store response time in cache for aggregation
            cache_key = 'metrics:response_times'
            response_times = cache.get(cache_key, [])
            response_times.append(response_time)
            
            # Keep only last 100 response times
            if len(response_times) > 100:
                response_times = response_times[-100:]
            
            cache.set(cache_key, response_times, 300)  # 5 minute TTL
            
            # Track cache hits/misses
            if response.status_code == 200:
                cache.incr('metrics:cache_hits', 1)
            
        return response
    
    def process_exception(self, request, exception):
        """Track exceptions and error rates."""
        logger.error(f"Exception in request: {exception}", exc_info=True)
        
        # Increment error counter
        cache.incr('metrics:cache_misses', 1)
        
        return None
