<div class="grid">
  <%= render Components::Backend::Card.new(title: "#{@identity.first_name} #{@identity.last_name}") do %>
    <%= render Components::Backend::Item.new(icon: "⭠", href: backend_identities_path) do %>
      <b>back to identities</b>
    <% end %>
    <% break_glass_tool do %>
      <hr>
      <%= render Components::Backend::Item.new(icon: "✎", href: edit_backend_identity_path(@identity)) do %>
        <b>edit identity</b>
      <% end %>
      <% if @identity.slack_id.present? %>
        <%= button_to promote_to_full_user_backend_identity_path(@identity), method: :post, class: "item" do %>
          <figure class="icon">↑</figure>
          <span class="text"><b>promote to full slack user</b></span>
        <% end %>
      <% end %>
    <% end %>
    <hr>
    <% if @identity.permabanned %>
      <div class="banner danger">
        <h3>⛔ permabanned</h3>
        <p>this identity is permanently banned.</p>
      </div>
    <% end %>
    <div class="section">
      <div class="section-header"><h3>identity</h3></div>
      <div class="section-content">
        <%= render Components::Identity.new(@identity, show_legal_name: @available_scopes.include?("legal_name")) %>
      </div>
    </div>
    <% if @available_scopes.include?("address") && @addresses.any? %>
      <div class="section">
        <div class="section-header"><h3>addresses</h3></div>
        <div class="section-content">
          <div class="table-container">
            <table>
              <thead>
                <tr>
                  <th>name</th>
                  <th>address</th>
                  <th>city</th>
                  <th>country</th>
                  <th>primary</th>
                </tr>
              </thead>
              <tbody>
                <% @addresses.each do |address| %>
                  <tr>
                    <td><%= address.first_name %> <%= address.last_name %></td>
                    <td><%= address.line_1 %><% if address.line_2.present? %><br><%= address.line_2 %><% end %></td>
                    <td><%= address.city %>, <%= address.state %> <%= address.postal_code %></td>
                    <td><%= address.country %></td>
                    <td><%= @identity.primary_address == address ? "✓" : "—" %></td>
                  </tr>
                <% end %>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    <% end %>
    <div class="section">
      <div class="section-header">
        <h3>verifications</h3>
        <% super_admin_tool do %>
          <%= link_to new_vouch_backend_identity_path(@identity) do %>
            <%= render Components::Backend::ActionButton.new { "+ VOUCH" } %>
          <% end %>
        <% end %>
      </div>
      <div class="section-content">
        <% if @verifications.any? %>
          <div class="table-container">
            <table>
              <thead>
                <tr>
                  <th>type</th>
                  <th>status</th>
                  <th>submitted</th>
                  <th>reason</th>
                  <th></th>
                </tr>
              </thead>
              <tbody>
                <% @verifications.each do |verification| %>
                  <tr>
                    <td>
                      <% case verification %>
                      <% when Verification::AadhaarVerification %>
                        Aadhaar
                      <% when Verification::DocumentVerification %>
                        <%= Identity::Document::FRIENDLY_NAMES[verification.identity_document.document_type.to_sym] %>
                      <% when Verification::VouchVerification %>
                        Vouch
                      <% end %>
                      <% if verification.ignored_at.present? %>
                        <span class="badge warning">ignored</span>
                      <% end %>
                    </td>
                    <td>
                      <span class="badge <%= verification.pending? ? 'pending' : verification.approved? ? 'success' : 'danger' %>">
                        <%= verification.status %>
                      </span>
                    </td>
                    <td>
                      <% if verification.respond_to?(:pending_at) && verification.pending_at %>
                        <%= verification.pending_at.strftime("%b %d, %Y") %>
                      <% else %>
                        <%= verification.created_at.strftime("%b %d, %Y") %>
                      <% end %>
                    </td>
                    <td>
                      <% if verification.ignored_at.present? %>
                        <%= verification.ignored_reason %>
                      <% elsif verification.rejection_reason.present? %>
                        <%= verification.rejection_reason_name %>
                      <% else %>
                        —
                      <% end %>
                    </td>
                    <td><%= link_to "view →", backend_verification_path(verification) %></td>
                  </tr>
                <% end %>
              </tbody>
            </table>
          </div>
        <% else %>
          <div class="empty-state">no verifications submitted</div>
        <% end %>
      </div>
    </div>
    <div class="section">
      <div class="section-header"><h3>programs</h3></div>
      <div class="section-content">
        <% if @all_programs.any? %>
          <div class="table-container">
            <table>
              <thead>
                <tr>
                  <th>program</th>
                  <th>scopes</th>
                </tr>
              </thead>
              <tbody>
                <% @all_programs.each do |program| %>
                  <tr>
                    <td><%= link_to program.name, backend_program_path(program) %></td>
                    <td><code><%= program.scopes.presence || "—" %></code></td>
                  </tr>
                <% end %>
              </tbody>
            </table>
          </div>
        <% else %>
          <div class="empty-state">no programs accessed</div>
        <% end %>
      </div>
    </div>
    <div class="section">
      <div class="section-header"><h3>audit log</h3></div>
      <div class="section-content">
        <% if @activities.any? %>
          <%= render Components::PublicActivity::Container.new(@activities) %>
        <% else %>
          <div class="empty-state">no audit entries</div>
        <% end %>
      </div>
    </div>
  <% end %>
  <%= render Components::Inspector.new(@identity) %>
</div>
