#!/usr/bin/env bash
# 살아 있는지 한 번: 서버 상태 · 관리자 로그인 3 · 메뉴 트리 · 회원 로그인 2 → GET /release/visible. 비밀번호 · 토큰은 찍지 않는다.
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
. "$here/lib.sh"
API=http://127.0.0.1:$SRV_PORT
J=$L/run/jar; mkdir -p "$J"
echo "live: $(curl -s -o /dev/null -w '%{http_code}' "$API/live")"
while IFS=$'\t' read -r kind id login pw; do
  jar=$J/$kind$id
  if [ "$kind" = adm ]; then
    body=$(jq -nc --arg l "$login" --arg p "$pw" '{admLoginId:$l, admPassword:$p}')
    code=$(curl -s -o "$jar.out" -w '%{http_code}' -c "$jar" -H 'content-type: application/json' -H "origin: http://localhost:$ADM_PORT" -d "$body" "$API/admin/auth/signin")
    menu=$(curl -s -b "$jar" "$API/admin/menu/my" | jq -r '[.. | objects | select(has("mnuCode")) | .mnuCode // ""] | "items=\(length) loss=\([.[] | select(startswith("MNU_LOSS_ANALYSIS"))] | join(","))"' 2>/dev/null || echo "menu 읽기 실패")
    rel=$(curl -s -o /dev/null -w '%{http_code}' -b "$jar" "$API/admin/release")
    echo "adm $id signin=$code menu/my: $menu  GET /admin/release=$rel"
  else
    body=$(jq -nc --arg l "$login" --arg p "$pw" '{loginType:"MEM001", loginId:$l, password:$p}')
    code=$(curl -s -o "$jar.out" -w '%{http_code}' -c "$jar" -H 'content-type: application/json' -H "origin: http://localhost:$SITE_PORT" -d "$body" "$API/auth/signin")
    vis=$(curl -s -b "$jar" "$API/release/visible" | jq -c '.flags')
    sub=$(curl -s -b "$jar" "$API/subscribe/$id" | jq -c '{subState: .subscribe.subState, ordProductType: .order.ordProductType, ordExpiredAt: .order.ordExpiredAt}')
    echo "mem $id signin=$code subscribe=$sub"
    echo "  visible=$vis"
  fi
  rm -f "$jar.out"
done < "$L/accounts.tsv"
