<div class="grid">
  <%= render Components::Backend::Card.new(title: "document review") do %>
    <%= render Components::Backend::Item.new(icon: "⭠", href: pending_backend_verifications_path) do %>
      <b>back to pending</b>
    <% end %>
    <hr>
    <div class="two-col">
      <div class="section">
        <div class="section-header"><h3>identity</h3></div>
        <div class="section-content">
          <%= render Components::IdentityReview::BasicDetails.new(@verification.identity) %>
        </div>
      </div>
      <div class="section">
        <div class="section-header"><h3>document</h3></div>
        <div class="section-content">
          <% case @verification %>
          <% when Verification::DocumentVerification %>
            <%= render Components::IdentityReview::DocumentInfo.new(@verification) %>
          <% when Verification::AadhaarVerification %>
            <%= render Components::IdentityReview::AadhaarInfo.new(@verification) %>
          <% end %>
        </div>
      </div>
    </div>
    <% if @verification.identity.resemblances.any? %>
      <div class="banner warning">
        <h3>⚠ possible duplicate</h3>
        <p>this identity may be a duplicate of another identity.</p>
        <% @verification.identity.resemblances.each do |resemblance| %>
          <%= render Components::Resemblance.new(resemblance) %>
        <% end %>
      </div>
    <% end %>
    <% if @verification.issues.any? %>
      <div class="banner info">
        <h3>automated checks noticed:</h3>
        <ul>
          <% @verification.issues.each do |issue| %>
            <li><%= issue %></li>
          <% end %>
        </ul>
      </div>
    <% end %>
    <div class="section">
      <div class="section-header"><h3>files</h3></div>
      <div class="section-content">
        <% if @verification.is_a?(Verification::VouchVerification) %>
          <p>manually vouched on <%= @verification.created_at.strftime("%B %d, %Y at %I:%M %p") %></p>
          <% if @verification.evidence.attached? %>
            <img src="<%= url_for(@verification.evidence) %>" alt="vouch evidence" style="max-width: 100%;">
          <% else %>
            <div class="empty-state">no evidence attached</div>
          <% end %>
        <% else %>
          <%= render Components::BreakTheGlass.new(@relevant_object, auto_break_glass: @verification.pending? ? "to review validity" : nil) do %>
            <% case @relevant_object %>
            <% when Identity::Document %>
              <%= render Components::IdentityReview::DocumentFiles.new(@relevant_object) %>
            <% when Identity::AadhaarRecord %>
              <%= render Components::IdentityReview::AadhaarFull.new(@relevant_object) %>
            <% end %>
          <% end %>
        <% end %>
      </div>
    </div>
    <hr>
    <% if @verification.pending? %>
      <div class="section">
        <div class="section-header"><h3>decision</h3></div>
        <div class="section-content">
          <div class="button_group full">
            <% mdv_tool do %>
              <%= form_with url: approve_backend_verification_path(@verification), method: :patch, local: true do |form| %>
                <%= form.hidden_field :ysws_eligible, value: true %>
                <%= form.submit "APPROVE (YSWS)", class: "action_button", data: { confirm: "approve and mark ysws eligible?" } %>
              <% end %>
              <%= form_with url: approve_backend_verification_path(@verification), method: :patch, local: true do |form| %>
                <%= form.hidden_field :ysws_eligible, value: false %>
                <%= form.submit "APPROVE (NOT YSWS)", class: "action_button", data: { confirm: "approve but mark ysws ineligible?" } %>
              <% end %>
            <% end %>
          </div>
          <% if @verification.identity.birthday < 19.years.ago %>
            <div class="banner warning">
              <p>⚠ birthday (<%= @verification.identity.birthday %>) is more than 19 years ago</p>
            </div>
          <% end %>
          <hr>
          <% mdv_tool do %>
            <%= form_with url: reject_backend_verification_path(@verification), method: :patch, local: true do |form| %>
              <% if @verification.identity.under_13? %>
                <div class="banner warning">
                  <p>⚠ identity is under 13 (age <%= @verification.identity.age.round(2) %>)</p>
                </div>
              <% end %>
              <div class="form-row">
                <%= form.label :rejection_reason, "reason" %>
                <% if @verification.is_a?(Verification::AadhaarVerification) %>
                  <%
                    default_selection = if @verification.identity.under_13?
                      "under_13"
                    elsif @verification.identity.resemblances.any?
                      "duplicate"
                    else
                      nil
                    end
                  %>
                  <%= form.select :rejection_reason,
                                 grouped_options_for_select([
                                   ["Retryable", Verification::AadhaarVerification::RETRYABLE_REJECTION_REASONS.map { |reason| [Verification::AadhaarVerification::REJECTION_REASON_NAMES[reason], reason] }],
                                   ["Fatal", Verification::AadhaarVerification::FATAL_REJECTION_REASONS.map { |reason| [Verification::AadhaarVerification::REJECTION_REASON_NAMES[reason], reason] }]
                                 ], default_selection),
                                 { include_blank: "select..." } %>
                <% elsif @verification.is_a?(Verification::DocumentVerification) %>
                  <%
                    default_selection = if @verification.identity.under_13?
                      "under_13"
                    elsif @verification.identity.resemblances.any?
                      "duplicate"
                    else
                      nil
                    end
                  %>
                  <%= form.select :rejection_reason,
                                 grouped_options_for_select([
                                   ["Retryable", Verification::DocumentVerification::RETRYABLE_REJECTION_REASONS.map { |reason| [Verification::DocumentVerification::REJECTION_REASON_NAMES[reason], reason] }],
                                   ["Fatal", Verification::DocumentVerification::FATAL_REJECTION_REASONS.map { |reason| [Verification::DocumentVerification::REJECTION_REASON_NAMES[reason], reason] }]
                                 ], default_selection),
                                 { include_blank: "select..." } %>
                <% end %>
              </div>
              <div class="form-row">
                <%= form.label :rejection_reason_details, "details" %>
                <%= form.text_area :rejection_reason_details, rows: 2, placeholder: "feedback for user..." %>
              </div>
              <div class="form-row">
                <%= form.label :internal_rejection_comment, "internal" %>
                <%= form.text_area :internal_rejection_comment, rows: 2, placeholder: "internal notes (not visible to user)..." %>
              </div>
              <div class="form-actions">
                <%= form.submit "REJECT", class: "action_button", data: { confirm: "reject this document?" } %>
              </div>
            <% end %>
          <% end %>
          <hr>
          <div class="detail-row">
            <span class="detail-label">current ysws status</span>
            <span class="detail-value">
              <% if @verification.identity.ysws_eligible.nil? %>
                <span class="badge pending">not reviewed</span>
              <% elsif @verification.identity.ysws_eligible %>
                <span class="badge success">eligible</span>
              <% else %>
                <span class="badge danger">ineligible</span>
              <% end %>
            </span>
          </div>
        </div>
      </div>
    <% else %>
      <div class="section">
        <div class="section-header"><h3>processed</h3></div>
        <div class="section-content lowered">
          <div class="detail-row">
            <span class="detail-label">status</span>
            <span class="detail-value"><span class="badge <%= @verification.approved? ? 'success' : 'danger' %>"><%= @verification.status %></span></span>
          </div>
          <% if @verification.rejection_reason.present? %>
            <div class="detail-row">
              <span class="detail-label">reason</span>
              <span class="detail-value"><%= @verification.rejection_reason_name %></span>
            </div>
            <% if @verification.rejection_reason_details.present? %>
              <div class="detail-row">
                <span class="detail-label">details</span>
                <span class="detail-value"><%= @verification.rejection_reason_details %></span>
              </div>
            <% end %>
            <% if @verification.internal_rejection_comment.present? %>
              <div class="detail-row">
                <span class="detail-label">internal</span>
                <span class="detail-value"><i><%= @verification.internal_rejection_comment %></i></span>
              </div>
            <% end %>
          <% end %>
        </div>
      </div>
    <% end %>
    <% if @verification.ignored_at.blank? %>
      <% super_admin_tool do %>
        <div class="section">
          <div class="section-header"><h3>⚠ super admin</h3></div>
          <div class="section-content">
            <%= form_with url: ignore_backend_verification_path(@verification), method: :patch, local: true do |f| %>
              <div class="form-row">
                <%= f.label :reason, "ignore reason" %>
                <%= f.text_area :reason, placeholder: "reason for ignoring (logged in audit trail)...", rows: 2 %>
              </div>
              <div class="form-actions">
                <%= f.submit "ignore verification", class: "action_button", data: { confirm: "ignore this verification? this cannot be undone." } %>
              </div>
            <% end %>
          </div>
        </div>
      <% end %>
    <% elsif @verification.ignored_at.present? %>
      <div class="section">
        <div class="section-header"><h3>⛔ ignored</h3></div>
        <div class="section-content lowered">
          <div class="detail-row">
            <span class="detail-label">ignored on</span>
            <span class="detail-value"><%= @verification.ignored_at.strftime("%B %d, %Y at %I:%M %p") %></span>
          </div>
          <div class="detail-row">
            <span class="detail-label">reason</span>
            <span class="detail-value"><%= @verification.ignored_reason %></span>
          </div>
        </div>
      </div>
    <% end %>
    <hr>
    <div class="section">
      <div class="section-header"><h3>audit log</h3></div>
      <div class="section-content">
        <%= render Components::PublicActivity::Container.new(@activities) %>
      </div>
    </div>
  <% end %>
  <%= render Components::Inspector.new(@verification) %>
  <%= render Components::Inspector.new(@relevant_object) %>
</div>
