<% post_file = post.primary_file_for_view || return%>
<% return unless (content_type = post_file.content_type) && is_thumbable_content_type?(content_type) %>
<section class="sky-section">
  <div class="section-header">Visually Similar Posts</div>
  <div class="grid grid-cols-[auto,auto,1fr,auto] bg-slate-100">
    <% fprint = post.primary_file_for_view&.bit_fingerprints&.first %>
    <% fprint_value = fprint&.fingerprint_value %>
    <% fprint_detail_value = fprint&.fingerprint_detail_value %>
    <% fprints = fprint && fprint_value && fprint_detail_value && find_similar_fingerprints(
            fingerprint_value: fprint_value,
            fingerprint_detail_value: fprint_detail_value,
            limit: 5,
            oversearch: 5,
        )
        .reject { |f| f.fingerprint.id == fprint.id }
        .reject { |f| f.similarity_percentage < 70 } || []
    %>
    <% if fprint.nil? %>
      <div class="col-span-full p-4 text-center text-slate-500">File not processed</div>
    <% elsif post_file && post_file.state_terminal_error? %>
      <div class="col-span-full p-4 text-center text-slate-500">File not found</div>
    <% elsif fprints.any? %>
      <% num_neighbors = fprints.size %>
      <% fprints.each_with_index do |similar_fingerprint, index| %>
        <% border_classes = index < num_neighbors - 1 ? "border-b border-slate-300" : "" %>
        <% post = similar_fingerprint.fingerprint.post_file.post %>
        <% creator = post.class.has_creators? ? post.creator : nil %>
        <div class="text-md items-center pl-4 pr-2 py-1 flex justify-end <%= border_classes %>">
          <div class="w-full text-center font-medium <%= match_badge_classes(similar_fingerprint.similarity_percentage) %>">
            <%= similar_fingerprint.similarity_percentage %>%
          </div>
        </div>
        <div class="flex items-center <%= border_classes %>">
          <% source_url = post.external_url_for_view&.to_s %>
          <% source_url = source_url && Addressable::URI.parse(source_url).host %>
          <% icon_path = source_url && icon_path_for_domain(source_url) %>
          <%= image_tag icon_path, class: "w-6 h-6 mr-2" if icon_path %>
        </div>
        <div class="text-md flex items-center pr-2 py-1 <%= border_classes %>">
          <%= render "domain/has_description_html/inline_link_domain_post", post: post, visual_style: "sky-link" %>
        </div>
        <% if creator %>
          <div class="text-md items-center px-4 py-1 <%= border_classes %>">
            <%= render "domain/has_description_html/inline_link_domain_user", user: creator, visual_style: "sky-link", icon_size: "large" %>
          </div>
        <% else %>
          <div class="text-md truncate px-4 py-1 <%= border_classes %>">
            <%= post.primary_creator_name_fallback_for_view %>
          </div>
        <% end %>
      <% end %>
    <% else %>
      <div class="col-span-full p-4 text-center text-slate-500">No visually similar posts found</div>
    <% end %>
  </div>
</section>
