{{!log 'chat-message' this}}
{{!log 'weaponDamageOptions' weaponDamageOptions}}
<div class="{{cssClass}}">
  <!-- Header with character info -->
  <div class="chat-header">
    <div class="character-info">
      <div class="character-avatar">
        <img src="{{actingCharImg}}" data-tooltip="{{actingCharName}}" />
      </div>
      <div class="character-details">
        <div class="character-name">{{actingCharName}}</div>
        <div class="roll-type-badge">
          {{#if (match rollType "attack")}}
            <i class="fa-solid fa-swords"></i>
          {{/if}}
          {{#if (match rollType "defense")}}
            <i class="fa-solid fa-shield"></i>
          {{/if}}
          {{#if rollData.isDamage}}
            <i class="fa-solid fa-burst"></i>
          {{/if}}
          {{#if (eq rollType "skill")}}
            <i class="fa-solid fa-hand-sparkles"></i>
          {{/if}}
          {{upperFirst rollName}}
        </div>
      </div>
    </div>
  </div>

  <!-- Roll details and modifiers -->
  <div class="roll-details">
    {{#if rollTarget.weapon}}
      <div class="detail-item weapon-name">
        <i class="fa-solid fa-sword"></i>
        <span>{{rollTarget.weapon.name}}</span>
      </div>
    {{/if}}

    {{#if (eq rollData.favor "favor")}}
      <div class="detail-badge favor-badge">
        <i class="fa-solid fa-sparkles"></i>
        <span>{{localize "LETHALFANTASY.Label.favor"}}</span>
      </div>
    {{/if}}
    {{#if (eq rollData.favor "disfavor")}}
      <div class="detail-badge disfavor-badge">
        <i class="fa-solid fa-skull"></i>
        <span>{{localize "LETHALFANTASY.Label.disfavor"}}</span>
      </div>
    {{/if}}

    {{#if rollData.letItFly}}
      <div class="detail-badge special-badge">
        <i class="fa-solid fa-bow-arrow"></i>
        <span>{{localize "LETHALFANTASY.Label.letItFly"}}</span>
      </div>
    {{/if}}
    {{#if rollData.pointBlank}}
      <div class="detail-badge special-badge">
        <i class="fa-solid fa-bullseye-arrow"></i>
        <span>{{localize "LETHALFANTASY.Label.pointBlank"}}</span>
      </div>
    {{/if}}
    {{#if rollData.beyondSkill}}
      <div class="detail-badge special-badge">
        <i class="fa-solid fa-target-lock"></i>
        <span>{{localize "LETHALFANTASY.Label.beyondSkill"}}</span>
      </div>
    {{/if}}

    {{#if rollData.isDamage}}
      <div class="detail-badge damage-badge">
        <i class="fa-solid fa-dice-d20"></i>
        <span>{{localize "LETHALFANTASY.Label.weapon-damage"}}</span>
      </div>
    {{/if}}

    {{#if badResult}}
      <div class="detail-item other-result">
        <strong>{{localize "LETHALFANTASY.Label.otherResult"}}</strong>: {{badResult}}
      </div>
    {{/if}}
  </div>

  <!-- Formula and dice results -->
  <div class="formula-section">
    <div class="formula-display">
      <i class="fa-solid fa-dice-d20"></i>
      <span class="formula-text">{{titleFormula}}</span>
    </div>
    {{#if diceResults}}
      <div class="dice-breakdown">
        {{#each diceResults as |result|}}
          <div class="dice-item">
            <span class="dice-type">{{result.dice}}</span>
            <span class="dice-separator">→</span>
            <span class="dice-value">{{result.value}}</span>
          </div>
        {{/each}}
      </div>
    {{/if}}
  </div>
  <!-- Result section -->
  {{#unless isPrivate}}
    <div class="result-section">
      <div class="main-result">
        <div class="result-label">{{localize "LETHALFANTASY.Label.total"}}</div>
        <div class="result-value {{#if (eq resultType 'success')}}success{{else}}failure{{/if}}">
          {{total}}
        </div>
      </div>

      {{#if D30result}}
        <div class="d30-result">
          <div class="d30-header">
            <i class="fa-solid fa-dice-d20"></i>
            <span class="d30-label">D30 Special</span>
            <span class="d30-value">{{D30result}}</span>
          </div>
          {{#if D30message}}
            <div class="d30-message">
              <i class="fa-solid fa-wand-magic-sparkles"></i>
              <span>{{D30message.description}}</span>
            </div>
          {{/if}}
        </div>
      {{/if}}
    </div>

    {{#if isSave}}
      <div class="outcome-badge {{#if (eq resultType 'success')}}success-outcome{{else}}failure-outcome{{/if}}">
        {{#if (eq resultType "success")}}
          <i class="fa-solid fa-circle-check"></i>
          {{localize "LETHALFANTASY.Roll.success"}}
        {{else}}
          <i class="fa-solid fa-circle-xmark"></i>
          {{localize "LETHALFANTASY.Roll.failure"}}
        {{/if}}
      </div>
    {{/if}}

    {{#if isResource}}
      <div class="outcome-badge {{#if (eq resultType 'success')}}success-outcome{{else}}failure-outcome{{/if}}">
        {{#if (eq resultType "success")}}
          <i class="fa-solid fa-circle-check"></i>
          {{localize "LETHALFANTASY.Roll.success"}}
        {{else}}
          <i class="fa-solid fa-circle-xmark"></i>
          {{localize "LETHALFANTASY.Roll.failure"}}
          {{#if isFailure}}
            <span class="resource-lost">({{localize "LETHALFANTASY.Roll.resourceLost"}})</span>
          {{/if}}
        {{/if}}
      </div>
    {{/if}}

    {{#if isDamage}}
      {{#if (and isGM hasTarget)}}
        <div class="target-info">
          <i class="fa-solid fa-crosshairs"></i>
          {{{localize
            "LETHALFANTASY.Roll.displayArmor"
            targetName=targetName
            targetArmor=targetArmor
            realDamage=realDamage
          }}}
        </div>
      {{/if}}
    {{/if}}
  {{else}}
    <div class="private-result">
      <i class="fa-solid fa-eye-slash"></i>
      <span>{{localize "LETHALFANTASY.Label.privateRoll"}}</span>
    </div>
  {{/unless}}

  {{#if weaponDamageOptions}}
    {{#if canSelectTarget}}
      <div class="damage-buttons">
        <div class="damage-buttons-grid {{#if weaponDamageOptions.isMonster}}monster-damage{{/if}}">
          {{#if weaponDamageOptions.isMonster}}
            <button
              class="damage-roll-btn"
              data-weapon-id="{{weaponDamageOptions.weaponId}}"
              data-damage-type="monster"
              data-damage-formula="{{weaponDamageOptions.damageFormula}}"
              data-damage-modifier="{{weaponDamageOptions.damageModifier}}"
              data-is-monster="true"
              title="{{localize 'LETHALFANTASY.Label.rollDamage'}} - {{weaponDamageOptions.weaponName}}"
            >
              <i class="fa-solid fa-dice"></i>
              Damage:
              {{weaponDamageOptions.damageFormula}}{{#if
                weaponDamageOptions.damageModifier
              }}+{{weaponDamageOptions.damageModifier}}{{/if}}
            </button>
          {{else}}
            {{#if weaponDamageOptions.damageM}}
              <button
                class="damage-roll-btn"
                data-weapon-id="{{weaponDamageOptions.weaponId}}"
                data-damage-type="medium"
                data-damage-formula="{{weaponDamageOptions.damageM}}"
                data-is-monster="false"
                title="{{localize 'LETHALFANTASY.Label.rollDamage'}}"
              >
                <i class="fa-solid fa-dice-d20"></i>
                {{localize "LETHALFANTASY.Label.damage"}}
              </button>
            {{/if}}
          {{/if}}
        </div>
      </div>
    {{/if}}
  {{/if}}

  {{#if rollData.isDamage}}
    {{#if defenderId}}
      <div class="damage-result auto-applied">
        <div class="auto-damage-notice">
          <i class="fa-solid fa-check-circle"></i>
          <span>Damage automatically applied to defender (with damage reduction)</span>
        </div>
      </div>
    {{else}}
      <div class="damage-result">
        <ul>
          <li class="li-apply-wounds">
            <div>{{localize "LETHALFANTASY.Label.applyDamage"}}</div>
            <div class="combatants-grid">
              {{#each combatants}}
                <button
                  class="apply-wounds-btn"
                  data-combatant-id="{{this.id}}"
                  title="{{this.name}}"
                >
                  {{this.name}}
                </button>
              {{/each}}
            </div>
          </li>
        </ul>
      </div>
    {{/if}}
  {{/if}}

  {{#if isAttack}}
    {{#if canSelectTarget}}
      <div class="attack-targets">
        <ul>
          <li class="li-select-target">
            <div>{{localize "LETHALFANTASY.Label.selectTarget"}}</div>
            <div class="combatants-grid">
              {{#each combatants}}
                <button
                  class="request-defense-btn"
                  data-combatant-id="{{this.id}}"
                  data-token-id="{{this.tokenId}}"
                  title="{{this.name}}"
                >
                  {{this.name}}
                </button>
              {{/each}}
            </div>
          </li>
        </ul>
      </div>
    {{/if}}
  {{/if}}

</div>
