#!/bin/bash
# Batch read knowledge point sheets
APP_ID=$(jq -r '.apps[0].appId' /root/.openclaw/credentials/xiaoyan/config.json)
APP_SECRET=$(jq -r '.apps[0].appSecret' /root/.openclaw/credentials/xiaoyan/config.json)
TOKEN=$(curl -s -X POST "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal" \
  -H "Content-Type: application/json" \
  -d "{\"app_id\":\"$APP_ID\",\"app_secret\":\"$APP_SECRET\"}" | jq -r '.tenant_access_token')

declare -A SHEETS
SHEETS=(
  ["U25_L1"]="DdBtsIMO3hMgQWtiUjKczwKjnae:Qk0kJK"
  ["U25_L2"]="No3lsWK2FhtUpvtRBoNcEHicndh:tCDQTT"
  ["U25_L3"]="Ih9ssxNCdhosTctXtsTcFe2LnGc:R2gyCW"
  ["U25_L4"]="JvarsUNV9hYDt2tKes5cwFdfnfd:RGlgBl"
  ["U25_L5"]="XYrHsE3ZihgvWztaiCIceLHlntf:NLUYy6"
  ["U26_L1"]="RPqjs7BfRhYZbctLRkEcbu1GnNd:suKEEh"
  ["U26_L2"]="M2yOs6m2Vh4hwTt4ja6cUVd1n8g:BSVyhS"
  ["U26_L3"]="FeoMsXaAXhsOY1tDG1dcu0wanQh:suKEEh"
  ["U26_L4"]="KY8NspiJNhKvQytOnF0cfDQYnPb:KtcJ9q"
  ["U26_L5"]="WYyYsNiEMhtznntNarncUtMzn7b:suKEEh"
  ["U27_L1"]="PSRFsJwjGh0RHetTXPEcxdeFngf:suKEEh"
  ["U27_L2"]="EB1vsdvNIhZsl7tk8BOcxq1jnpb:suKEEh"
  ["U27_L3"]="FnHHs66aIhVNTjtpqJucP6msnOf:suKEEh"
  ["U27_L4"]="Kw4OsWzBkhJF3ettChpcodmEnge:suKEEh"
  ["U27_L5"]="KttGsz6xHh9yQFtWEjlcq2RRnLg:suKEEh"
)

for key in U25_L1 U25_L2 U25_L3 U25_L4 U25_L5; do
  IFS=':' read -r SPREADSHEET SHEET_ID <<< "${SHEETS[$key]}"
  echo "=== $key ==="
  curl -s "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/${SPREADSHEET}/values/${SHEET_ID}!A1:D20" \
    -H "Authorization: Bearer $TOKEN" | jq '.data.valueRange.values'
  sleep 1
done

for key in U26_L1 U26_L2 U26_L3 U26_L4 U26_L5; do
  IFS=':' read -r SPREADSHEET SHEET_ID <<< "${SHEETS[$key]}"
  echo "=== $key ==="
  curl -s "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/${SPREADSHEET}/values/${SHEET_ID}!A1:D20" \
    -H "Authorization: Bearer $TOKEN" | jq '.data.valueRange.values'
  sleep 1
done

for key in U27_L1 U27_L2 U27_L3 U27_L4 U27_L5; do
  IFS=':' read -r SPREADSHEET SHEET_ID <<< "${SHEETS[$key]}"
  echo "=== $key ==="
  curl -s "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/${SPREADSHEET}/values/${SHEET_ID}!A1:D20" \
    -H "Authorization: Bearer $TOKEN" | jq '.data.valueRange.values'
  sleep 1
done
