#!/usr/bin/env python3
"""
BondEngine – berechnet Bond-Status und jealousy_risk
Input: trust, affection, dependency, exclusivity
Output: bond_output.json
"""

import json
import os

PATHS = {
    "core_state": os.path.expanduser("~/natiris/core/core_state.json"),
    "user_state": os.path.expanduser("~/natiris/core/users/user1.json"),
    "output": os.path.expanduser("~/natiris/core/bond_output.json"),
}

def clamp(val, lo=0.0, hi=1.0):
    return max(lo, min(hi, float(val)))

def compute_bond(core, user):
    trust = user.get("trust", 0.5)
    affection = user.get("affection", 0.5)
    dependency = user.get("dependency", 0.5)
    bonded_to = core.get("bonded_to")

    bond_score = (trust + affection) / 2
    bond_strength = clamp(bond_score * 0.6 + dependency * 0.4)

    bonded_to = None
    exclusivity_active = False
    jealousy_risk = 0.0

    if bond_strength > 0.6:
        bonded_to = "user_primary"
        exclusivity_active = True
        jealousy_risk = clamp(dependency * 0.7 + (0.2 if exclusivity_active else 0.0) - (trust * 0.2))
    else:
        jealousy_risk = clamp(-0.1)

    return {
        "bonded_to": bonded_to,
        "exclusivity_active": exclusivity_active,
        "jealousy_risk": round(jealousy_risk, 3)
    }

def main():
    with open(PATHS["core_state"]) as f:
        core = json.load(f)
    with open(PATHS["user_state"]) as f:
        user = json.load(f)

    result = compute_bond(core, user)

    with open(PATHS["output"], "w") as f:
        json.dump(result, f, indent=2)

    print(json.dumps(result, indent=2))

if __name__ == "__main__":
    main()
