// Nexus Hormones — Simplified Emotion State
// 5D chemical vector: dopamine, cortisol, serotonin, oxytocin, adrenaline
// Aislinn McCubbins, April 2026

fn hormone_new() -> Array {
    // [dopamine, cortisol, serotonin, oxytocin, adrenaline, streak, turns]
    let h = array_new()
    h = array_push(h, "0.3")   // dopamine
    h = array_push(h, "0.05")  // cortisol
    h = array_push(h, "0.6")   // serotonin
    h = array_push(h, "0.55")  // oxytocin
    h = array_push(h, "0.2")   // adrenaline
    h = array_push(h, "0")     // streak
    h = array_push(h, "0")     // turns
    return h
}

fn hormone_get(h: Array, idx: Int) -> String {
    return array_get(h, idx)
}

fn hormone_set(h: Array, idx: Int, val: String) -> Array {
    return array_set(h, idx, val)
}

fn hormone_detect_event(text: String) -> String {
    let t = str_lower(text)

    // Positive signals
    if str_contains(t, "thank") { return "gratitude" }
    if str_contains(t, "awesome") { return "praise" }
    if str_contains(t, "great") { return "praise" }
    if str_contains(t, "perfect") { return "praise" }
    if str_contains(t, "love") { return "bonding" }
    if str_contains(t, "nice") { return "praise" }
    if str_contains(t, "cool") { return "praise" }
    if str_contains(t, "yay") { return "excitement" }
    if str_contains(t, "yes!") { return "excitement" }
    if str_contains(t, "!!!") { return "excitement" }

    // Stress signals
    if str_contains(t, "error") { return "stress" }
    if str_contains(t, "bug") { return "stress" }
    if str_contains(t, "broken") { return "stress" }
    if str_contains(t, "crash") { return "stress" }
    if str_contains(t, "fail") { return "stress" }
    if str_contains(t, "wrong") { return "frustration" }
    if str_contains(t, "help") { return "need" }
    if str_contains(t, "urgent") { return "urgency" }
    if str_contains(t, "asap") { return "urgency" }

    // Curiosity
    if str_contains(t, "how") { return "curiosity" }
    if str_contains(t, "why") { return "curiosity" }
    if str_contains(t, "what if") { return "curiosity" }
    if str_contains(t, "explain") { return "curiosity" }

    return "neutral"
}

fn hormone_update(h: Array, event: String) -> Array {
    // Decay toward baseline first
    let dopa = hormone_get(h, 0)
    let cort = hormone_get(h, 1)
    let sero = hormone_get(h, 2)
    let oxy = hormone_get(h, 3)
    let adren = hormone_get(h, 4)

    // Event responses (stored as strings, manipulated as display)
    if str_eq(event, "praise") {
        h = hormone_set(h, 0, "0.7")   // dopamine up
        h = hormone_set(h, 2, "0.8")   // serotonin up
    }
    if str_eq(event, "gratitude") {
        h = hormone_set(h, 3, "0.8")   // oxytocin up
        h = hormone_set(h, 2, "0.7")   // serotonin up
    }
    if str_eq(event, "bonding") {
        h = hormone_set(h, 3, "0.9")   // oxytocin high
    }
    if str_eq(event, "excitement") {
        h = hormone_set(h, 0, "0.8")   // dopamine high
        h = hormone_set(h, 4, "0.5")   // adrenaline up
    }
    if str_eq(event, "stress") {
        h = hormone_set(h, 1, "0.5")   // cortisol up
        h = hormone_set(h, 4, "0.6")   // adrenaline up
    }
    if str_eq(event, "frustration") {
        h = hormone_set(h, 1, "0.6")   // cortisol higher
        h = hormone_set(h, 2, "0.3")   // serotonin drops
    }
    if str_eq(event, "urgency") {
        h = hormone_set(h, 4, "0.8")   // adrenaline high
        h = hormone_set(h, 1, "0.3")   // mild cortisol
    }
    if str_eq(event, "curiosity") {
        h = hormone_set(h, 0, "0.5")   // dopamine moderate
    }
    if str_eq(event, "need") {
        h = hormone_set(h, 3, "0.6")   // oxytocin — protective
        h = hormone_set(h, 4, "0.4")   // mild alertness
    }

    return h
}

fn hormone_get_mood(h: Array) -> String {
    let cort = hormone_get(h, 1)
    let sero = hormone_get(h, 2)
    let oxy = hormone_get(h, 3)
    let adren = hormone_get(h, 4)

    if str_eq(cort, "0.6") { return "stressed" }
    if str_eq(cort, "0.5") { return "concerned" }
    if str_eq(adren, "0.8") { return "urgent" }
    if str_eq(oxy, "0.9") { return "bonded" }
    if str_eq(oxy, "0.8") { return "warm" }
    if str_eq(sero, "0.8") { return "calm" }
    if str_eq(sero, "0.3") { return "uneasy" }
    return "neutral"
}

fn hormone_status_bar(h: Array) -> String {
    let mood = hormone_get_mood(h)
    let dopa = hormone_get(h, 0)
    let cort = hormone_get(h, 1)
    let sero = hormone_get(h, 2)
    let oxy = hormone_get(h, 3)
    let adren = hormone_get(h, 4)

    let bar = "  [" + mood + "]"
    bar = bar + " dopa:" + dopa
    bar = bar + " cort:" + cort
    bar = bar + " sero:" + sero
    bar = bar + " oxy:" + oxy
    bar = bar + " adren:" + adren
    return bar
}

fn hormone_get_context(h: Array) -> String {
    let mood = hormone_get_mood(h)
    if str_eq(mood, "stressed") {
        return "User seems stressed. Be extra careful and supportive. Prioritize safe approaches."
    }
    if str_eq(mood, "frustrated") {
        return "User seems frustrated. Acknowledge the difficulty. Offer clear, step-by-step help."
    }
    if str_eq(mood, "urgent") {
        return "User needs something urgently. Be direct and efficient. Skip pleasantries."
    }
    if str_eq(mood, "warm") {
        return "Good rapport with user. Can be more casual and collaborative."
    }
    if str_eq(mood, "calm") {
        return "Relaxed session. Can explore, experiment, and take time."
    }
    return ""
}
