"""
Weighted Objective Function Hook
Auto-generated by Atomizer Phase 2.9

Combine normalized stress (70%) and displacement (30%)

Inputs: norm_stress, norm_disp
Weights: 0.7, 0.3
Formula: 0.7 * norm_stress + 0.3 * norm_disp
Objective: minimize
"""

import sys
import json
from pathlib import Path


def weighted_objective(norm_stress, norm_disp):
    """
    Calculate weighted objective from multiple inputs.

    Args:
        norm_stress: float
        norm_disp: float

    Returns:
        float: Weighted objective value
    """
    result = 0.7 * norm_stress + 0.3 * norm_disp
    return result


def main():
    """
    Main entry point for hook execution.
    Reads inputs from JSON file, calculates objective, writes output.
    """
    # Parse command line arguments
    if len(sys.argv) < 2:
        print("Usage: python {} <input_file.json>".format(sys.argv[0]))
        sys.exit(1)

    input_file = Path(sys.argv[1])

    # Read inputs
    if not input_file.exists():
        print(f"Error: Input file {input_file} not found")
        sys.exit(1)

    with open(input_file, 'r') as f:
        inputs = json.load(f)

    # Extract required inputs
    norm_stress = inputs.get("norm_stress")
    if norm_stress is None:
        print(f"Error: Required input 'norm_stress' not found")
        sys.exit(1)
    norm_disp = inputs.get("norm_disp")
    if norm_disp is None:
        print(f"Error: Required input 'norm_disp' not found")
        sys.exit(1)

    # Calculate weighted objective
    result = weighted_objective(norm_stress, norm_disp)

    # Write output
    output_file = input_file.parent / "weighted_objective_result.json"
    output = {
        "weighted_objective": result,
        "objective_type": "minimize",
        "inputs_used": {"norm_stress": norm_stress, "norm_disp": norm_disp},
        "formula": "0.7 * norm_stress + 0.3 * norm_disp"
    }

    with open(output_file, 'w') as f:
        json.dump(output, f, indent=2)

    print(f"Weighted objective calculated: {result:.6f}")
    print(f"Result saved to: {output_file}")

    return result


if __name__ == '__main__':
    main()
