"""
Combine normalized stress (70%) and displacement (30%)
Auto-generated lifecycle hook by Atomizer Phase 2.9

Hook Point: post_calculation
Inputs: norm_stress, norm_disp
Outputs: weighted_objective
"""

import logging
from typing import Dict, Any, Optional

logger = logging.getLogger(__name__)


def weighted_objective_hook(context: Dict[str, Any]) -> Optional[Dict[str, Any]]:
    """
    Combine normalized stress (70%) and displacement (30%)

    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 weighted_objective_hook for trial {context.get('trial_number', 'unknown')}")

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

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

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

    # Calculate weighted objective
    result = 0.7 * norm_stress + 0.3 * norm_disp

    logger.info(f"Weighted objective calculated: {result:.6f}")

    return {
        'weighted_objective': result,
        'weighted_objective': 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=weighted_objective_hook,
        description="Combine normalized stress (70%) and displacement (30%)",
        name="weighted_objective_hook",
        priority=100,
        enabled=True
    )
    logger.info(f"Registered weighted_objective_hook at post_calculation")
