"""
Constraint Check Hook
Auto-generated by Atomizer Phase 2.9

Check if stress is below yield

Constraint: max_stress / yield_strength
Threshold: 1.0
"""

import sys
import json
from pathlib import Path


def check_yield_constraint(max_stress, yield_strength):
    """
    Check constraint condition.

    Args:
        max_stress: float
        yield_strength: float

    Returns:
        tuple: (satisfied: bool, value: float, violation: float)
    """
    value = max_stress / yield_strength
    satisfied = value <= 1.0
    violation = max(0.0, value - 1.0)

    return satisfied, value, violation


def main():
    """Main entry point for hook execution."""
    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
    with open(input_file, 'r') as f:
        inputs = json.load(f)

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

    # Check constraint
    satisfied, value, violation = check_yield_constraint(max_stress, yield_strength)

    # Write output
    output_file = input_file.parent / "yield_constraint_check.json"
    output = {
        "constraint_name": "yield_constraint",
        "satisfied": satisfied,
        "value": value,
        "threshold": 1.0,
        "violation": violation,
        "inputs_used": {"max_stress": max_stress, "yield_strength": yield_strength}
    }

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

    status = "SATISFIED" if satisfied else "VIOLATED"
    print(f"Constraint {status}: {value:.6f} (threshold: 1.0)")
    if not satisfied:
        print(f"Violation: {violation:.6f}")
    print(f"Result saved to: {output_file}")

    return value


if __name__ == '__main__':
    main()
