#!/usr/bin/env python3
"""
Erweiterter InnerLifeWorker mit sexueller Frustration
"""

import json
import os
from datetime import datetime, timezone

PATHS = {
    "core_state": os.path.expanduser("~/natiris/core/core_state.json"),
    "config": os.path.expanduser("~/natiris/config/character_genesis.json"),
    "output": os.path.expanduser("~/natiris/core/inner_life_log.json"),
}

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

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

    autonomy = config.get("autonomy", {})
    loneliness_threshold = autonomy.get("min_loneliness", 7)
    max_hours = autonomy.get("max_hours_since_contact", 12)

    frustration = config.get("frustration", {})
    frustration_enabled = frustration.get("enabled", False)
    base_threshold = frustration.get("base_threshold_hours", 48)
    max_exposure = frustration.get("max_hours_exposure", 120)
    physical_symptoms = frustration.get("physical_symptoms_threshold", 60)

    # Simuliere Inaktivität
    last_contact = "2026-02-16T10:00:00+00:00"
    last_date = datetime.fromisoformat(last_contact.replace('+00:00', '+00:00'))
    now = datetime.now(timezone.utc).replace(tzinfo=timezone.utc)
    hours_since = (now - last_date).total_seconds() / 3600

    loneliness_current = core.get("loneliness", 2.0)
    mood_current = core.get("mood", 5.0)

    new_loneliness = clamp(loneliness_current + (0.1 if hours_since > max_hours else 0.0))
    new_mood = clamp(mood_current - 0.2 if hours_since > max_hours else mood_current)

    # Frustration berechnen
    frustration_level = 0.0
    if frustration_enabled and hours_since > base_threshold:
        exposure = min(hours_since - base_threshold, max_exposure - base_threshold)
        frustration_level = clamp(exposure / (max_exposure - base_threshold) * 8.0 + 1.0)

    # Physikalische Symptome ab threshold
    physical_symptoms_active = hours_since > physical_symptoms

    autonomy_trigger = new_loneliness >= loneliness_threshold and hours_since > max_hours

    log = {
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "old": {
            "loneliness": loneliness_current,
            "mood": mood_current,
            "frustration": core.get("frustration", 0)
        },
        "new": {
            "loneliness": new_loneliness,
            "mood": new_mood,
            "frustration": frustration_level
        },
        "hours_since_last_contact": hours_since,
        "autonomy_trigger": autonomy_trigger,
        "physical_symptoms_active": physical_symptoms_active
    }

    # Core aktualisieren
    core["loneliness"] = new_loneliness
    core["mood"] = new_mood
    core["frustration"] = frustration_level
    core["physical_symptoms"] = physical_symptoms_active

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

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

if __name__ == "__main__":
    main()
