#!/bin/bash
set -e

MANAGER="http://127.0.0.1:19088"
TOKEN="dev-admin-token"
PROFILE_ID="profile-c92edf65-a"

echo "=== Updating Config Profile with model_key in candidates ==="

curl -sS -X PUT -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{
    "name": "YunWo Brain State",
    "cluster_id": "yunwocloud-4af3c7",
    "priority": 0,
    "resources": [
      {"kind": "runtime.key", "key": "brain.enabled", "value": true},
      {"kind": "runtime.key", "key": "brain.context_window_size", "value": 200000},
      {"kind": "llm.channel", "resource_uid": "channel/llm-proxy", "value": {"name": "llm-proxy", "protocol": "openai", "api_base": "http://127.0.0.1:39527/v1", "credential_configured": true}},
      {"kind": "llm.model", "resource_uid": "model/llm-proxy/glm-5-2", "value": {"model_key": "glm-5-2", "enabled": true, "supports_tools": false}},
      {"kind": "llm.model", "resource_uid": "model/llm-proxy/nemotron", "value": {"model_key": "nvidia/nemotron-3-super-120b-a12b:free", "enabled": true, "supports_tools": false}},
      {"kind": "llm.group", "resource_uid": "group/default", "value": {"name": "Default", "members": ["channel/llm-proxy"]}},
      {"kind": "llm.route", "resource_uid": "route/brain-thinking", "value": {"purpose": "brain_thinking", "candidates": [{"target": "group/default", "position": 0}]}},
      {"kind": "llm.route", "resource_uid": "route/context-compression", "value": {"purpose": "context_compression", "candidates": [{"target": "channel/llm-proxy", "model_key": "nvidia/nemotron-3-super-120b-a12b:free", "position": 0}]}},
      {"kind": "llm.route", "resource_uid": "route/memory-narrative", "value": {"purpose": "memory_narrative", "candidates": [{"target": "channel/llm-proxy", "model_key": "nvidia/nemotron-3-super-120b-a12b:free", "position": 0}]}},
      {"kind": "llm.route", "resource_uid": "route/memory-generator", "value": {"purpose": "memory_generator", "candidates": [{"target": "channel/llm-proxy", "model_key": "nvidia/nemotron-3-super-120b-a12b:free", "position": 0}]}},
      {"kind": "llm.route", "resource_uid": "route/memory-episode-extractor", "value": {"purpose": "memory_episode_extractor", "candidates": [{"target": "channel/llm-proxy", "model_key": "nvidia/nemotron-3-super-120b-a12b:free", "position": 0}]}},
      {"kind": "llm.route", "resource_uid": "route/memory-cognition-extractor", "value": {"purpose": "memory_cognition_extractor", "candidates": [{"target": "channel/llm-proxy", "model_key": "nvidia/nemotron-3-super-120b-a12b:free", "position": 0}]}},
      {"kind": "llm.route", "resource_uid": "route/recall-memory-summary", "value": {"purpose": "recall_memory_summary", "candidates": [{"target": "channel/llm-proxy", "model_key": "nvidia/nemotron-3-super-120b-a12b:free", "position": 0}]}},
      {"kind": "llm.route", "resource_uid": "route/memory-cognition-dedup", "value": {"purpose": "memory_cognition_dedup", "candidates": [{"target": "channel/llm-proxy", "model_key": "nvidia/nemotron-3-super-120b-a12b:free", "position": 0}]}},
      {"kind": "llm.route", "resource_uid": "route/memory-step-experience", "value": {"purpose": "memory_step_experience_extractor", "candidates": [{"target": "channel/llm-proxy", "model_key": "nvidia/nemotron-3-super-120b-a12b:free", "position": 0}]}},
      {"kind": "llm.route", "resource_uid": "route/memory-task-experience", "value": {"purpose": "memory_task_experience_summarizer", "candidates": [{"target": "channel/llm-proxy", "model_key": "nvidia/nemotron-3-super-120b-a12b:free", "position": 0}]}},
      {"kind": "llm.route", "resource_uid": "route/memory-operation-detail", "value": {"purpose": "memory_operation_detail_learner", "candidates": [{"target": "channel/llm-proxy", "model_key": "nvidia/nemotron-3-super-120b-a12b:free", "position": 0}]}}
    ],
    "issued_by": "admin"
  }' \
  "$MANAGER/api/v1/config-profiles/$PROFILE_ID" 2>/dev/null

echo ""
echo "Waiting for config convergence..."
sleep 15

echo ""
echo "=== Instance status ==="
for INSTANCE in friend-043bd076 friend-4f864178; do
  echo "--- $INSTANCE ---"
  curl -sS -H "Authorization: Bearer $TOKEN" "$MANAGER/api/v1/instances/$INSTANCE" 2>/dev/null | python3 -c "
import json,sys
d=json.load(sys.stdin)
inst=d.get('instance',d)
print('  config_state:', inst.get('config_state',''))
print('  health_state:', inst.get('health_state',''))
print('  last_error:', str(inst.get('last_error',''))[:300])
"
done
