<% content_for :title, "#{@letter.user_facing_title || 'Letter'} #{@letter.public_id}" %>

<div class="container">
  <header class="page-header mb-6">
    <div class="flex items-center gap-4">
      <%= link_to "← Back", letters_path, class: "text-gray-500 hover:text-gray-700" %>
      <div>
        <h1 class="flex items-center gap-3">
          <% if @letter.user_facing_title.present? %>
            <%= @letter.user_facing_title %>
          <% else %>
            Letter to <%= @letter.address.name_line %>
          <% end %>
          <%= letter_status_badge(@letter.aasm_state) %>
        </h1>
        <div class="text-sm text-gray-500"><%= @letter.public_id %></div>
      </div>
    </div>
    <div class="actions flex gap-2">
      <%= link_to edit_letter_path(@letter), class: "btn btn-small" do %>
        Edit
      <% end %>
      <%= link_to public_letter_path(@letter), target: "_blank", class: "btn btn-small" do %>
        Public Page
      <% end %>
      <%= button_to letter_path(@letter), method: :delete, class: "btn danger btn-small", data: { confirm: "Delete this letter?" } do %>
        Delete
      <% end %>
    </div>
  </header>

  <div class="grid md:grid-cols-12 gap-6">
    <%# Main content %>
    <div class="md:col-span-8 space-y-6">
      <%# Addresses %>
      <article>
        <header><h2>Addresses</h2></header>
        <div class="p-4">
          <%= render 'shared/tags', tags: @letter.tags %>
          <div class="grid md:grid-cols-2 gap-4">
            <div>
              <div class="text-sm text-gray-500 mb-1">From</div>
              <% if @letter.return_address.present? %>
                <address class="p-3 bg-gray-50 rounded not-italic text-sm">
                  <div class="font-medium"><%= @letter.return_address_name_line %></div>
                  <div><%= @letter.return_address.line_1 %></div>
                  <% if @letter.return_address.line_2.present? %>
                    <div><%= @letter.return_address.line_2 %></div>
                  <% end %>
                  <div><%= @letter.return_address.city %>, <%= @letter.return_address.state %> <%= @letter.return_address.postal_code %></div>
                  <div><%= @letter.return_address.country %></div>
                </address>
              <% else %>
                <div class="p-3 bg-gray-50 rounded text-gray-500 text-sm">No return address</div>
              <% end %>
            </div>
            <div>
              <div class="text-sm text-gray-500 mb-1">To</div>
              <address class="p-3 bg-gray-50 rounded not-italic text-sm">
                <div class="font-medium"><%= @letter.address.name_line %></div>
                <div><%= @letter.address.line_1 %></div>
                <% if @letter.address.line_2.present? %>
                  <div><%= @letter.address.line_2 %></div>
                <% end %>
                <div><%= @letter.address.city %>, <%= @letter.address.state %> <%= @letter.address.postal_code %></div>
                <div><%= @letter.address.country %></div>
              </address>
            </div>
          </div>
        </div>
      </article>

      <%# Specifications %>
      <article>
        <header><h2>Specifications</h2></header>
        <div class="p-4">
          <%= render 'shared/letter_attributes', record: @letter %>
        </div>
      </article>

      <%# Postage %>
      <article>
        <header><h2>Postage</h2></header>
        <div class="p-4">
          <% if @letter.postage_type == "indicia" %>
            <% if @letter.usps_indicium.present? %>
              <div class="flex items-center gap-2 text-green-700 mb-4">
                <span class="font-medium">✓ Indicia Purchased</span>
              </div>
              <dl class="grid md:grid-cols-2 gap-4 text-sm">
                <div>
                  <dt class="text-gray-500">Cost</dt>
                  <dd class="font-medium">
                    <%= number_to_currency(@letter.usps_indicium.cost) %>
                    <span class="text-gray-500 font-normal">(<%= number_to_currency(@letter.usps_indicium.postage) %> + <%= number_to_currency(@letter.usps_indicium.fees || 0) %> fees)</span>
                  </dd>
                </div>
                <div>
                  <dt class="text-gray-500">Mailing Date</dt>
                  <dd><%= @letter.usps_indicium.mailing_date %></dd>
                </div>
                <div>
                  <dt class="text-gray-500">USPS SKU</dt>
                  <dd class="font-mono text-xs"><%= @letter.usps_indicium.usps_sku %></dd>
                </div>
                <div>
                  <dt class="text-gray-500">Indicium ID</dt>
                  <dd class="font-mono text-xs"><%= @letter.usps_indicium.public_id %></dd>
                </div>
              </dl>
              <%= render 'admin_inspector', record: @letter.usps_indicium %>
            <% else %>
              <div class="text-yellow-700 mb-4">
                <span class="font-medium">Indicia Not Purchased</span>
              </div>
              <% if @letter.batch_id.nil? %>
                <%= render 'buy_indicia_form', letter: @letter %>
              <% else %>
                <p class="text-sm text-gray-600">Postage will be purchased when processing the batch.</p>
              <% end %>
            <% end %>
          <% elsif @letter.postage_type == "international_origin" %>
            <p class="text-sm text-gray-600">
              <strong>International Origin</strong> — Postage paid at origin (<%= @letter.return_address&.country %>)
            </p>
          <% else %>
            <div class="flex items-center justify-between">
              <p class="text-sm text-gray-600">Using physical stamps</p>
              <% if @letter.batch_id.nil? %>
                <%= button_to letter_path(@letter), method: :patch, params: { letter: { postage_type: "indicia" } }, class: "btn btn-small" do %>
                  Switch to Indicia
                <% end %>
              <% end %>
            </div>
          <% end %>
        </div>
      </article>

      <% if @letter.batch.present? %>
        <%= render 'shared/batch_info', record: @letter %>
      <% end %>

      <% if @letter.imb_serial_number.present? && @letter.address.country&.upcase == 'US' %>
        <article>
          <header><h2>IMb Tracking</h2></header>
          <div class="p-4">
            <dl class="grid md:grid-cols-2 gap-4 text-sm">
              <div>
                <dt class="text-gray-500">Serial</dt>
                <dd class="font-mono"><%= @letter.imb_serial_number.to_s.rjust(6, '0') %></dd>
              </div>
              <div>
                <dt class="text-gray-500">Epoch</dt>
                <dd><%= @letter.imb_rollover_count %></dd>
              </div>
            </dl>
          </div>
        </article>
      <% end %>

      <% if @letter.rubber_stamps.present? %>
        <article>
          <header><h2>Rubber Stamps</h2></header>
          <div class="p-4">
            <pre class="text-sm bg-gray-50 p-3 rounded overflow-auto"><%= @letter.rubber_stamps %></pre>
          </div>
        </article>
      <% end %>

      <%= render partial: "admin_inspector", locals: { record: @letter } %>
    </div>

    <%# Sidebar %>
    <div class="md:col-span-4">
      <article class="sticky top-4">
        <header><h2>Actions</h2></header>
        <div class="p-4 space-y-4">
          <% case @letter.aasm_state %>
          <% when 'pending' %>
            <% if @letter.label.attached? %>
              <div>
                <div class="text-sm text-gray-500 mb-2">Label Ready</div>
                <div class="flex flex-wrap gap-2">
                  <%= link_to rails_blob_path(@letter.label, disposition: 'inline'), class: "btn btn-small success", target: "_blank" do %>
                    View Label
                  <% end %>
                  <%= button_to clear_label_letter_path(@letter), method: :post, class: "btn btn-small", data: { confirm: "Clear this label?" } do %>
                    Clear
                  <% end %>
                </div>
              </div>
              <hr class="my-4">
              <%= button_to mark_printed_letter_path(@letter), method: :post, class: "btn success w-full" do %>
                Mark as Printed
              <% end %>
            <% elsif @letter.batch.present? %>
              <%= button_to mark_printed_letter_path(@letter), method: :post, class: "btn success w-full" do %>
                Mark as Printed
              <% end %>
            <% else %>
              <div>
                <div class="text-sm text-gray-500 mb-2">Generate Label</div>
                <%= form_tag(generate_label_letter_path(@letter), method: :post) do %>
                  <div class="mb-3">
                    <%= render 'shared/template_picker', multiple: false %>
                    <input type="hidden" name="template" id="selected_template">
                  </div>
                  <label class="flex items-center gap-2 text-sm mb-3">
                    <input type="checkbox" name="qr" value="1" checked>
                    Include QR Code
                  </label>
                  <button type="submit" class="btn success w-full">Generate Label</button>
                <% end %>
                <% dev_tool do %>
                  <%= link_to "preview label?", preview_template_letter_path(@letter), class: "text-sm" %>
                <% end %>
              </div>
            <% end %>
          <% when 'printed' %>
            <%= button_to mark_mailed_letter_path(@letter), method: :post, class: "btn success w-full" do %>
              Mark as Mailed
            <% end %>
          <% when 'mailed' %>
            <%= button_to mark_received_letter_path(@letter), method: :post, class: "btn success w-full" do %>
              Mark as Received
            <% end %>
          <% when 'received' %>
            <div class="text-center text-green-600 py-4">
              ✓ Letter received
            </div>
          <% end %>
        </div>
      </article>

      <% if @letter.label.attached? %>
        <%= render 'shared/instant_print_window', url: rails_blob_path(@letter.label, disposition: 'inline') %>
        <article class="mt-4">
          <header><h2>Label Preview</h2></header>
          <div class="p-4">
            <div class="border rounded overflow-hidden" style="height: 250px;">
              <iframe src="<%= rails_blob_path(@letter.label, disposition: 'inline') %>" class="w-full h-full border-0"></iframe>
            </div>
            <div class="mt-3">
              <%= link_to rails_blob_path(@letter.label, disposition: "attachment"), class: "text-sm" do %>
                Download Label
              <% end %>
            </div>
          </div>
        </article>
      <% end %>
    </div>
  </div>
</div>
