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

echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║   TC-18 — Dialogues                                        ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

tc_ensure_ready

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

# ============================================================================
echo "--- TC-18-014: Dialogues V2 — window positioning ---"
# ============================================================================
R=$(jse_get_async "
  try {
    // Open a DialogV2 and check it has window controls (position, resize)
    const dlg = new foundry.applications.api.DialogV2({
      title: 'TC-18 Test Dialog',
      content: '<p>Test</p>',
      buttons: [{action: 'close', label: 'Fermer', callback: (e,b) => b.close()}]
    });
    await dlg.render(true);
    await new Promise(r => setTimeout(r, 1000));
    const el = dlg.element;
    const hasWindow = !!el?.querySelector('.window-header');
    const hasControls = !!el?.querySelector('.window-controls, .close, .header-button');
    await dlg.close();
    return { hasWindow, hasControls };
  } catch(e) { return {error: e.message}; }
")
HASWIN=$(eval_ok "$R" hasWindow)
ERR=$(eval_ok "$R" error)
[ "$HASWIN" = "True" ] && pass "TC-18-014: dialog V2 avec window header" || fail "TC-18-014: KO ($ERR)"

# ============================================================================
echo "--- TC-18-015: Dialogues V2 — boutons (au moins 1 bouton) ---"
# ============================================================================
R=$(jse_get_async "
  try {
    const dlg = new foundry.applications.api.DialogV2({
      title: 'TC-18 Test Buttons',
      content: '<p>Test</p>',
      buttons: [{action: 'ok', label: 'OK', callback: (e,b) => b.close()}]
    });
    await dlg.render(true);
    await new Promise(r => setTimeout(r, 1000));
    const btns = dlg.element?.querySelectorAll('button');
    const count = btns?.length ?? 0;
    await dlg.close();
    return { btnCount: count, ok: count >= 1 };
  } catch(e) { return {error: e.message}; }
")
OK=$(eval_ok "$R" ok)
ERR=$(eval_ok "$R" error)
[ "$OK" = "True" ] && pass "TC-18-015: dialog V2 a ≥1 bouton" || fail "TC-18-015: KO ($ERR)"

# Cleanup
jse_get_async "const a = game.actors.getName('TC-18 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