#!/usr/bin/env bash
# ============================================================================
# TC-09 — Système de Jet V2 (Roll Dialog)
# Usage: ./tc-09-roll-v2.sh [--navigate]
# ============================================================================
set -uo pipefail
source "$(dirname "$0")/tc-header.sh"

echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║   TC-09 — Système de Jet V2 (Roll Dialog)                   ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

tc_ensure_ready

# Setup
SETUP=$(jse_get_async "
  const ex = game.actors.getName('TC-09 Test');
  if (ex) await ex.delete();
  const [actor] = await Actor.createDocuments([{
    type: 'personnage', name: 'TC-09 Test',
    system: { carac: { force: {value: 12} }, sante: { vie: {value: 12, max: 12}, endurance: {value: 20, max: 20} } }
  }]);
  const [comp] = await actor.createEmbeddedDocuments('Item', [{
    type: 'competence', name: 'TC-09 Comp',
    system: { categorie: 'physique', niveau: 5, xp: 0, base: 0, carac: 'force' }
  }]);
  return { actorId: actor.id, compId: comp?.id };
")
AID=$(eval_ok "$SETUP" actorId)

# ============================================================================
echo "--- TC-09-001: Toggle V2 activé ---"
# ============================================================================
R=$(jse_get_async "
  try {
    const SETTING = 'rdd-advanced-roll-dialog-v2';
    const before = game.settings.get('foundryvtt-reve-de-dragon', SETTING);
    await game.settings.set('foundryvtt-reve-de-dragon', SETTING, true);
    const after = game.settings.get('foundryvtt-reve-de-dragon', SETTING);
    return { before, after, ok: after === true };
  } catch(e) { return {error: e.message}; }
")
OK=$(eval_ok "$R" ok)
ERR=$(eval_ok "$R" error)
[ "$OK" = "True" ] && pass "TC-09-001: ROLL_DIALOG_V2 activé" || fail "TC-09-001: KO ($ERR)"

# ============================================================================
echo "--- TC-09-002: Roll V2 — module chargé ---"
# ============================================================================
R=$(jse_get_async "
  try {
    const mod = await import('/systems/foundryvtt-reve-de-dragon/module/roll/roll-dialog.mjs');
    const RollDialog = mod.default ?? mod.RollDialog;
    return { ok: !!RollDialog, hasCreate: typeof RollDialog?.create === 'function' };
  } catch(e) { return {error: e.message}; }
")
OK=$(eval_ok "$R" ok)
ERR=$(eval_ok "$R" error)
[ "$OK" = "True" ] && pass "TC-09-002: RollDialog V2 module chargé" || fail "TC-09-002: KO ($ERR)"

# ============================================================================
echo "--- TC-09-005: Toggle V2 désactivé ---"
# ============================================================================
R=$(jse_get_async "
  try {
    const SETTING = 'rdd-advanced-roll-dialog-v2';
    await game.settings.set('foundryvtt-reve-de-dragon', SETTING, false);
    const after = game.settings.get('foundryvtt-reve-de-dragon', SETTING);
    return { after, ok: after === false };
  } catch(e) { return {error: e.message}; }
")
OK=$(eval_ok "$R" ok)
ERR=$(eval_ok "$R" error)
[ "$OK" = "True" ] && pass "TC-09-005: ROLL_DIALOG_V2 désactivé" || fail "TC-09-005: KO ($ERR)"

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