<section class="order-total-formula">
  <h3>Total formula</h3>

  @if (!hasFullBreakdown()) {
    <p class="order-total-formula__unavailable">
      Price breakdown not available for this order. Backend has not populated
      per-line unitPriceMinor / lineTotalMinor yet.
    </p>
  } @else {
    <table class="order-total-formula__table">
      <thead>
        <tr>
          <th>Line</th>
          <th>Qty</th>
          <th>Unit price</th>
          <th>Discount</th>
          <th>Line total</th>
        </tr>
      </thead>
      <tbody>
        @for (row of lineRows(); track row.name) {
          <tr>
            <td>{{ row.name }}</td>
            <td>{{ row.quantity }}</td>
            <td>{{ toDisplay(row.unitPriceMinor) }} {{ order().currency }}</td>
            <td>{{ row.discountMinor ? toDisplay(row.discountMinor) + ' ' + order().currency : '—' }}</td>
            <td>{{ toDisplay(row.lineTotalMinor) }} {{ order().currency }}</td>
          </tr>
        }
      </tbody>
    </table>

    <dl class="order-total-formula__summary">
      <div>
        <dt>Subtotal</dt>
        <dd>{{ toDisplay(subtotalMinor()) }} {{ order().currency }}</dd>
      </div>
      @if (totalDiscountMinor() > 0) {
        <div>
          <dt>Discounts</dt>
          <dd>&minus;{{ toDisplay(totalDiscountMinor()) }} {{ order().currency }}</dd>
        </div>
      }
      @if (order().deliveryMinor !== undefined) {
        <div>
          <dt>Delivery</dt>
          <dd>{{ toDisplay(order().deliveryMinor) }} {{ order().currency }}</dd>
        </div>
      }
      <div class="order-total-formula__grand">
        <dt>Charged total</dt>
        <dd>{{ order().total }} {{ order().currency }}</dd>
      </div>
      @if (order().fxQuoteId) {
        <div class="order-total-formula__fx">
          <dt>FX quote used</dt>
          <dd><code>{{ order().fxQuoteId }}</code></dd>
        </div>
      }
    </dl>
  }

  @if (order().routing; as routing) {
    <p class="order-total-formula__routing">
      Routed to payment point <code>{{ routing.leafNodeId }}</code> ({{ routing.environment }}) &middot;
      merchant reference <code>{{ routing.merchantReference }}</code>
    </p>
  }
</section>
