#!/usr/bin/env bash
# ============================================================================
# TC-16 — Commandes Chat
# Usage: ./tc-16-commandes-chat.sh [--navigate]
# ============================================================================
set -uo pipefail
source "$(dirname "$0")/tc-header.sh"

echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║   TC-16 — Commandes Chat                                     ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

tc_ensure_ready

# Helper: run a chat command and check for a chat message
test_chat_cmd() {
	local id="$1" cmd="$2" pattern="$3"
	local before after
	before=$(jse_get_async "return game.messages.size;" | tr -d '"\n')
	jse_get_async "
		const msg = {content: '$cmd', speaker: {alias: 'TC-16'}};
		game.system.rdd.commands.onChatMessage('$cmd', msg);
		await new Promise(r => setTimeout(r, 1500));
		return 'done';
	" >/dev/null 2>&1
	after=$(jse_get_async "return game.messages.size;" | tr -d '"\n')
	local newcount=$((after - before))
	local match
	match=$(jse_get_async "
		const msgs = game.messages.contents.slice(-$newcount);
		const found = msgs.some(m => (m.content || '').match(/$pattern/));
		return found;
	" | tr -d '"\n')
	[ "$match" = "true" ] && pass "$id: $cmd → match '$pattern'" || fail "$id: $cmd → no match (new=$newcount)"
}

# TC-16-001: /aide
echo "--- TC-16-001: /aide ---"
test_chat_cmd "TC-16-001" "/aide" "aide|help|command"

# TC-16-003: /table queue
echo "--- TC-16-003: /table queue ---"
test_chat_cmd "TC-16-003" "/table queue" "Queue|queue|dragon"

# TC-16-004: /table ombre
echo "--- TC-16-004: /table ombre ---"
test_chat_cmd "TC-16-004" "/table ombre" "Ombre|ombre|Thanatos"

# TC-16-005: /table tete
echo "--- TC-16-005: /table tete ---"
test_chat_cmd "TC-16-005" "/table tete" "[Tt]ête|Tete|Dragon|tirage"

# TC-16-006: /table souffle
echo "--- TC-16-006: /table souffle ---"
test_chat_cmd "TC-16-006" "/table souffle" "Souffle|souffle"

# TC-16-007: /table tarot
echo "--- TC-16-007: /table tarot ---"
test_chat_cmd "TC-16-007" "/table tarot" "[Tt]arot"

# TC-16-017: /meteo
echo "--- TC-16-017: /meteo ---"
test_chat_cmd "TC-16-017" "/meteo" "[Mm]étéo|[Vv]ent|[Mm]er|[Nn]uage|[Pp]luie"

# TC-16-018: /nom
echo "--- TC-16-018: /nom ---"
test_chat_cmd "TC-16-018" "/nom" "nom|Nom|proposition|Créer"

# TC-16-023: /ddr
echo "--- TC-16-023: /ddr ---"
test_chat_cmd "TC-16-023" "/ddr" "draconique|Dé|d[ée]|[Dd]ragon"

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

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

tc_summary