#!/bin/zsh
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"

GREEN='%F{green}'
RED='%F{red}'
YELLOW='%F{yellow}'
BLUE='%F{blue}'
BOLD='%B'
NC='%b%f'

CONFIG_FILE="$HOME/.openclaw/openclaw.json"
GOLDEN_REMOTE="$HOME/.openclaw/golden-remote.json"

clear
print -P "${BLUE}┌─────────────────────────────────────────────────────┐${NC}"
print -P "${BLUE}│ ${BOLD}🚀 回歸雲端：恢復 DeepSeek KK 線路${NC}${BLUE} │${NC}"
print -P "${BLUE}│ ═════════════════════════════════════════════════ │${NC}"

chflags nouchg "$CONFIG_FILE" 2>/dev/null
chmod 644 "$CONFIG_FILE" 2>/dev/null

if [ -f "$GOLDEN_REMOTE" ]; then
 cp "$GOLDEN_REMOTE" "$CONFIG_FILE"
 cp "$GOLDEN_REMOTE" "$HOME/.openclaw/kk-golden.json" 2>/dev/null
 print -P "${BLUE}│${NC} [$(date '+%H:%M:%S')] ${GREEN}⚙️ 已載入純淨 DeepSeek 專屬雲端設定${NC}"
else
 print -P "${BLUE}│${NC} [$(date '+%H:%M:%S')] ${RED}❌ 備份遺失，執行強制格式化寫入...${NC}"
 echo '{"models":{"providers":{}},"agents":{"defaults":{"workspace":"/Users/lingochen/.openclaw/workspace","models":{"deepseek/deepseek-v4-flash":{}},"model":{"primary":"deepseek/deepseek-v4-flash","fallbacks":["deepseek/deepseek-v4-flash"]},"thinkingDefault":"off"},"list":[]}}' > "$CONFIG_FILE"
fi

chmod 444 "$CONFIG_FILE"
chflags uchg "$CONFIG_FILE"

openclaw gateway restart > /dev/null 2>&1
sleep 2

print -P "${BLUE}│${NC}"
print -P "${BLUE}│${NC} [$(date '+%H:%M:%S')] ${GREEN}${BOLD}🔴 系統已完全回歸純淨 DeepSeek KK 線路！${NC}"
print -P "${BLUE}└─────────────────────────────────────────────────────┘${NC}"
echo ""

read "CONFIRM?按 Enter 離開並關閉視窗..."
osascript -e 'tell application "Terminal" to close first window' &
exit 0
