<h1>Problems</h1>

<% if @blocking.empty? && @no_po_cost.empty? && @backordered.empty? && @stuck_orders.empty? %>
  <p>no problems! go take a nap or something.</p>
<% end %>

<% if @blocking.any? %>
  <h2 style="color: red;">Dispatch blockers</h2>
  <p>These enabled SKUs have <strong>no cost data at all</strong> &mdash; no PO cost, no override. Any order containing them will refuse to dispatch.</p>
  <table class="table-fixed text-left border border-gray-300 border-collapse" style="width: 100%;">
    <tr>
      <th class="border border-gray-300">SKU</th>
      <th class="border border-gray-300">Name</th>
      <th class="border border-gray-300">In stock</th>
      <th class="border border-gray-300">Zenventory</th>
    </tr>
    <% @blocking.each do |sku| %>
      <tr>
        <td class="border border-gray-400"><%= link_to sku.sku, warehouse_sku_path(sku) %></td>
        <td class="border border-gray-400"><%= sku.name %></td>
        <td class="border border-gray-400"><%= sku.in_stock || "—" %></td>
        <td class="border border-gray-400">
          <% if sku.zenventory_url %>
            <%= link_to "view", sku.zenventory_url, target: "_blank" %>
          <% end %>
        </td>
      </tr>
    <% end %>
  </table>
<% end %>

<% if @no_po_cost.any? %>
  <h2 style="color: orange;">Missing PO cost data</h2>
  <p>These enabled SKUs have no <code>average_po_cost</code> from Zenventory but are covered by an override or other fallback. They work, but the PO data in Zenventory should be fixed.</p>
  <table class="table-fixed text-left border border-gray-300 border-collapse" style="width: 100%;">
    <tr>
      <th class="border border-gray-300">SKU</th>
      <th class="border border-gray-300">Name</th>
      <th class="border border-gray-300">Declared cost</th>
      <th class="border border-gray-300">Source</th>
      <th class="border border-gray-300">Zenventory</th>
    </tr>
    <% @no_po_cost.each do |sku| %>
      <tr>
        <td class="border border-gray-400"><%= link_to sku.sku, warehouse_sku_path(sku) %></td>
        <td class="border border-gray-400"><%= sku.name %></td>
        <td class="border border-gray-400"><%= number_to_currency sku.declared_unit_cost %></td>
        <td class="border border-gray-400">
          <% if sku.declared_unit_cost_override&.positive? %>
            override
          <% else %>
            ???
          <% end %>
        </td>
        <td class="border border-gray-400">
          <% if sku.zenventory_url %>
            <%= link_to "view", sku.zenventory_url, target: "_blank" %>
          <% end %>
        </td>
      </tr>
    <% end %>
  </table>
<% end %>

<% if @backordered.any? %>
  <h2 style="color: orange;">Backordered without enough inbound</h2>
  <p>These SKUs are oversold and don't have enough inbound stock to cover the deficit.</p>
  <table class="table-fixed text-left border border-gray-300 border-collapse" style="width: 100%;">
    <tr>
      <th class="border border-gray-300">SKU</th>
      <th class="border border-gray-300">Name</th>
      <th class="border border-gray-300">In stock</th>
      <th class="border border-gray-300">Inbound</th>
      <th class="border border-gray-300">Shortfall</th>
    </tr>
    <% @backordered.each do |sku| %>
      <tr>
        <td class="border border-gray-400"><%= link_to sku.sku, warehouse_sku_path(sku) %></td>
        <td class="border border-gray-400"><%= sku.name %></td>
        <td class="border border-gray-400" style="color: red;"><%= sku.in_stock %></td>
        <td class="border border-gray-400"><%= sku.inbound || 0 %></td>
        <td class="border border-gray-400" style="color: red;"><%= sku.in_stock.abs - (sku.inbound || 0) %></td>
      </tr>
    <% end %>
  </table>
<% end %>

<% if @stuck_orders.any? %>
  <h2 style="color: red;">Stuck orders</h2>
  <p>These orders were dispatched to the warehouse over 2 weeks ago but never marked as mailed. Something might be wrong with the Zenventory sync, or they're stuck at the warehouse.</p>
  <table class="table-fixed text-left border border-gray-300 border-collapse" style="width: 100%;">
    <tr>
      <th class="border border-gray-300">Order</th>
      <th class="border border-gray-300">Recipient</th>
      <th class="border border-gray-300">Dispatched</th>
      <th class="border border-gray-300">Age</th>
      <th class="border border-gray-300">Zenventory</th>
    </tr>
    <% @stuck_orders.each do |order| %>
      <tr>
        <td class="border border-gray-400"><%= link_to order.hc_id, warehouse_order_path(order) %></td>
        <td class="border border-gray-400"><%= order.recipient_email %></td>
        <td class="border border-gray-400"><%= order.dispatched_at.strftime("%b %d, %Y") %></td>
        <td class="border border-gray-400" style="color: red;"><%= distance_of_time_in_words_to_now(order.dispatched_at) %> ago</td>
        <td class="border border-gray-400">
          <% if order.zenventory_url %>
            <%= link_to "view", order.zenventory_url, target: "_blank" %>
          <% end %>
        </td>
      </tr>
    <% end %>
  </table>
<% end %>
