"""
Compare min force to average
Auto-generated lifecycle hook by Atomizer Phase 2.9

Hook Point: post_calculation
Inputs: min_force, avg_force
Outputs: min_to_avg_ratio
"""

import logging
from typing import Dict, Any, Optional

logger = logging.getLogger(__name__)


def ratio_hook(context: Dict[str, Any]) -> Optional[Dict[str, Any]]:
    """
    Compare min force to average

    Args:
        context: Hook context containing:
            - trial_number: Current optimization trial
            - results: Dictionary with extracted FEA results
            - calculations: Dictionary with inline calculation results

    Returns:
        Dictionary with calculated values to add to context
    """
    logger.info(f"Executing ratio_hook for trial {context.get('trial_number', 'unknown')}")

    # Extract inputs from context
    results = context.get('results', {})
    calculations = context.get('calculations', {})

    min_force = calculations.get('min_force') or results.get('min_force')
    if min_force is None:
        logger.error(f"Required input 'min_force' not found in context")
        raise ValueError(f"Missing required input: min_force")

    avg_force = calculations.get('avg_force') or results.get('avg_force')
    if avg_force is None:
        logger.error(f"Required input 'avg_force' not found in context")
        raise ValueError(f"Missing required input: avg_force")

    # Calculate comparison
    result = min_force / avg_force

    logger.info(f"min_to_avg_ratio = {result:.6f}")

    return {
        'min_to_avg_ratio': result
    }


def register_hooks(hook_manager):
    """
    Register this hook with the HookManager.

    This function is called automatically when the plugin is loaded.

    Args:
        hook_manager: The HookManager instance
    """
    hook_manager.register_hook(
        hook_point='post_calculation',
        function=ratio_hook,
        description="Compare min force to average",
        name="ratio_hook",
        priority=100,
        enabled=True
    )
    logger.info(f"Registered ratio_hook at post_calculation")
