#!/usr/bin/env bash
# ============================================================================
# TC-13 — Sommeil / Repos
# Usage: ./tc-13-sommeil.sh [--navigate]
# ============================================================================
set -uo pipefail
source "$(dirname "$0")/tc-header.sh"

echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║   TC-13 — Sommeil / Repos                                  ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

tc_ensure_ready

# Setup
SETUP=$(jse_get_async "
  const ex = game.actors.getName('TC-13 Test');
  if (ex) await ex.delete();
  const [actor] = await Actor.createDocuments([{
    type: 'personnage', name: 'TC-13 Test',
    system: { sante: { vie: {value: 12, max: 12}, endurance: {value: 20, max: 20} } }
  }]);
  return { actorId: actor.id };
")
AID=$(eval_ok "$SETUP" actorId)

# ============================================================================
echo "--- TC-13-001: Repos (ancien) /sommeil ---"
# ============================================================================
R=$(jse_get_async "
  try {
    game.system.rdd.commands.onChatMessage('/sommeil', {content: '/sommeil', speaker: {alias: 'TC-13'}});
    await new Promise(r => setTimeout(r, 2000));
    const el = document.querySelector('[class*=chateau-dormant], [class*=sommeil], .dialog-chateau-dormant');
    return { opened: !!el };
  } catch(e) { return {error: e.message}; }
")
OPENED=$(eval_ok "$R" opened)
ERR=$(eval_ok "$R" error)
[ "$OPENED" = "True" ] && pass "TC-13-001: dialog sommeil ouvert" || fail "TC-13-001: KO ($ERR)"
jse_get_async "
  const apps = Object.values(ui.windows).filter(w => w.constructor?.name?.includes('Chateau') || w.constructor?.name?.includes('Dormant') || w.constructor?.name?.includes('Sommeil'));
  for (const a of apps) await a.close();
  return 'closed';
" >/dev/null 2>&1

# ============================================================================
echo "--- TC-13-005: Distribution Stress GM /stress ---"
# ============================================================================
R=$(jse_get_async "
  try {
    game.system.rdd.commands.onChatMessage('/stress', {content: '/stress', speaker: {alias: 'TC-13'}});
    await new Promise(r => setTimeout(r, 2000));
    const el = document.querySelector('[class*=dialog-stress], [class*=stress]');
    return { opened: !!el };
  } catch(e) { return {error: e.message}; }
")
OPENED=$(eval_ok "$R" opened)
ERR=$(eval_ok "$R" error)
[ "$OPENED" = "True" ] && pass "TC-13-005: dialog stress ouvert" || fail "TC-13-005: KO ($ERR)"
jse_get_async "
  const apps = Object.values(ui.windows).filter(w => w.constructor?.name?.includes('Stress'));
  for (const a of apps) await a.close();
  return 'closed';
" >/dev/null 2>&1

# Cleanup
jse_get_async "const a = game.actors.getName('TC-13 Test'); if (a) await a.delete(); return 'ok';" >/dev/null 2>&1

echo ""
echo "--- Console errors ---"
ERRS=$(tc_console_errors)
[ "$ERRS" -le 5 ] 2>/dev/null && pass "Console errors ≤5 ($ERRS)" || fail "Console errors: $ERRS"

tc_summary