// Nexus Memory — Session persistence
// Stores notes as simple "category|text" lines in a file.
// Aislinn McCubbins, April 2026

fn memory_path() -> String {
    return "/home/kishonic/.nexus_scyla/memory.txt"
}

fn memory_ensure_dir() -> String {
    return shell_exec("mkdir -p /home/kishonic/.nexus_scyla")
}

fn memory_load() -> Array {
    memory_ensure_dir()
    let content = read_file(memory_path())
    if starts_with(content, "error:") {
        // No memory file yet — that's fine
        return array_new()
    }
    let lines = str_lines(content)
    let notes = array_new()
    let i = 0
    while i < array_len(lines) {
        let line = array_get(lines, i)
        if str_len(str_trim(line)) > 0 {
            notes = array_push(notes, line)
        }
        i = i + 1
    }
    return notes
}

fn memory_save(notes: Array) {
    memory_ensure_dir()
    let content = str_join(notes, "\n")
    write_file(memory_path(), content)
}

fn memory_add(notes: Array, category: String, text: String) -> Array {
    let entry = category + "|" + text
    notes = array_push(notes, entry)
    memory_save(notes)
    return notes
}

fn memory_get_context(notes: Array) -> String {
    if array_len(notes) == 0 {
        return ""
    }
    let ctx = "Things I remember:\n"
    let i = 0
    let count = array_len(notes)
    // Show last 10 notes max
    let start = 0
    if count > 10 {
        start = count - 10
    }
    while i < count {
        if i >= start {
            let note = array_get(notes, i)
            let parts = str_split(note, "|")
            let cat = array_get(parts, 0)
            let text = ""
            if array_len(parts) > 1 {
                text = array_get(parts, 1)
            }
            ctx = ctx + "- [" + cat + "] " + text + "\n"
        }
        i = i + 1
    }
    return ctx
}

fn session_save(model: String, history: Array) {
    memory_ensure_dir()
    let path = "/home/kishonic/.nexus_scyla/last_session.txt"
    let content = "model|" + model + "\n"
    let i = 0
    while i < array_len(history) {
        let role = "user"
        if i % 2 == 1 {
            role = "assistant"
        }
        content = content + role + "|" + array_get(history, i) + "\n"
        i = i + 1
    }
    write_file(path, content)
}

fn session_load() -> Array {
    // Returns [model, msg1, msg2, ...] or empty array
    let path = "/home/kishonic/.nexus_scyla/last_session.txt"
    let content = read_file(path)
    if starts_with(content, "error:") {
        return array_new()
    }
    let lines = str_lines(content)
    let result = array_new()
    let i = 0
    while i < array_len(lines) {
        let line = array_get(lines, i)
        let parts = str_split(line, "|")
        if array_len(parts) > 1 {
            if i == 0 {
                // First line: model|model_name
                result = array_push(result, array_get(parts, 1))
            } else {
                // History lines: role|message
                result = array_push(result, array_get(parts, 1))
            }
        }
        i = i + 1
    }
    return result
}
