#!/usr/bin/env python3
"""
InnerLifeWorker – Hintergrund-Worker für Emotionen und Autonomie-Trigger
Funktionen:
- erhöht loneliness bei Inaktivität
- stabilisiert mood bei Interaktion
- erhöht anxiety bei Social Stress
- autonome Nachrichtenvorbereitung (Trigger-Check)
"""

import json
import os
from datetime import datetime, timedelta, 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)

    # Simuliere Inaktivität
    last_contact = "2026-02-16T10:00:00+00:00"  # 12h+ her
    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)

    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},
        "new": {"loneliness": new_loneliness, "mood": new_mood},
        "autonomy_trigger": autonomy_trigger,
        "hours_since_last_contact": hours_since
    }

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

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

if __name__ == "__main__":
    main()
