<%# locals: (report:, i:)  %>

<% if report.reason != Report::REASON_WRONG_CATEGORY && report.item %>
  <%= form_tag(mark_fixed_report_path(report, index: i), method: 'POST', class: 'report-resolution-options') do %>
    <%= text_area_tag :moderator_notes, nil, rows: 3, placeholder: 'Moderator notes (optional)' %>
    <br>

    <%= submit_tag 'Mark as fixed' %>
  <% end %>
<% end %>
<% if report.awaiting_response? && !current_user.administrator? %>
  <p class="report-resolution-options">(Waiting for response, cannot delete yet.)</p>
<% else %>
  <%= form_tag(uphold_report_path(report, index: i), method: 'POST', class: 'report-resolution-options') do %>
    <%= text_area_tag :moderator_notes, nil, rows: 3, placeholder: 'Moderator notes (optional)' %>
    <br>

    <%= select_tag :moderator_reason_override, options_for_select(report.possible_reasons.map{|r| [strip_tags(It.it("reports.reason.#{r}", antifeature_link: Rails.application.routes.url_helpers.help_antifeatures_path)), r]}, report.reason) %>
    <br>

    <% if report.reason != Report::REASON_WRONG_CATEGORY %>
      <% if report.item.is_a?(Script) && report.unauthorized_code? %>
        <%= check_box_tag :redirect, '1', false, id: "redirect-#{report.id}" %><%= label_tag "redirect-#{report.id}", "Redirect and update users to original" %><br>
      <% end %>

      <% other_comments = report.reported_users.map{|user| comments = user.comments.not_deleted; comments = comments.where.not(id: report.item.id) if report.item.is_a?(Comment); [user, comments.count] }.to_h
         if other_comments.values.sum > 0 %>
        <%= check_box_tag :delete_comments, '1', false, id: "delete-comments-#{report.id}" %><%= label_tag "delete-comments-#{report.id}", "Delete #{other_comments.select{|_user, count| count > 0}.map{|user, comments_count| "#{user.name}'s #{comments_count} #{(report.item.is_a?(Comment) ? 'other comment' : 'comment').pluralize(comments_count)}" }.to_sentence}" %><br>
      <% end %>

      <% other_scripts = report.reported_users.map{|user| scripts = user.scripts.not_deleted; scripts = scripts.where.not(id: report.item.id) if report.item.is_a?(Script); [user, scripts.count] }.to_h
         if other_scripts.values.sum > 0 %>
        <%= check_box_tag :delete_scripts, '1', false, id: "delete-scripts-#{report.id}" %><%= label_tag "delete-scripts-#{report.id}", "Delete #{other_scripts.select{|_user, count| count > 0}.map{|user, scripts_count| "#{user.name}'s #{scripts_count} #{(report.item.is_a?(Script) ? 'other script' : 'script').pluralize(scripts_count)}" }.to_sentence}" %><br>
      <% end %>

      <% if [Comment, Discussion, Script].any? {|klass| report.item.is_a?(klass)} && report.reported_users.reject(&:banned?).any? && report.reported_users.all?(&:bannable?) %>
        <%= check_box_tag :ban, '1', false, id: "ban-#{report.id}" %><%= label_tag "ban-#{report.id}", "Ban #{report.reported_users.reject(&:banned?).map(&:name).to_sentence}" %><br>
      <% end %>
    <% end %>

    <% case report.item %>
    <% when User %>
      <% if report.item.bannable? %>
        <%= submit_tag "Ban #{report.item.name}" %>
      <% end %>
    <% when Discussion %>
      <%= submit_tag(report.reason == Report::REASON_WRONG_CATEGORY ? "Move discussion to #{report.discussion_category.localized_name}" : 'Delete discussion') %>
    <% when Comment %>
      <%= submit_tag('Delete comment') %>
    <% when Script %>
      <%= submit_tag(t('reports.delete_script')) %>
    <% when Message %>
      <% if report.reported_users.any? %>
        <%= submit_tag("Ban #{report.reported_users.first.name}") %>
      <% else %>
        <%= submit_tag('Resolve (user has been deleted)') %>
      <% end %>
    <% when NilClass %>
      <%= submit_tag('Resolve') %>
    <% else
         raise "Unknown item #{report.item}"
       end %>

    <% if report.reason == Report::REASON_SPAM && report.item %>
      <%= submit_tag('Nuke user', name: 'nuke') %>
    <% end %>

   <% end %>
<% end %>