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

<% show_reporter = current_user && (current_user.moderator? || current_user == report.reporter || report.reporter&.moderator?) %>

<h4 id="open-report-<%= i %>">
  <% if report.unauthorized_code? && (report.reference_script || report.script_url.present?) %>
    <% if show_reporter %>
      <%= t('reports.report_intro_unauthorized_html', reported_script: report_item_link(report), other_script: report.reference_script ? render_script(report.reference_script) : link_to(report.script_url, report.script_url, rel: :nofollow), reporter: report.auto_reporter || render_user(report.reporter, report.reporter_id), report_date: markup_date(report.created_at)) %>
    <% else %>
      <%= t('reports.report_intro_unauthorized_no_reporter_html', reported_script: report_item_link(report), other_script: report.reference_script ? render_script(report.reference_script) : link_to(report.script_url, report.script_url, rel: :nofollow), report_date: markup_date(report.created_at)) %>
    <% end %>
  <% else %>
    <% if show_reporter %>
      <%= it('reports.report_intro', reported_item: report_item_link(report), reason: report.reason_text, reporter: report.auto_reporter || render_user(report.reporter, report.reporter_id), report_date: markup_date(report.created_at)) %>
    <% else %>
      <%= it('reports.report_intro_no_reporter', reported_item: report_item_link(report), reason: report.reason_text, report_date: markup_date(report.created_at)) %>
    <% end %>
  <% end %>
</h4>

<% if report.unauthorized_code? && report.reference_script %>
  <p>
    <% if report.reference_script.license&.url %>
      <%= it('reports.report_intro_unauthorized_license',
             other_script: report.reference_script ? render_script(report.reference_script) : link_to(report.script_url, report.script_url, rel: :nofollow),
             license_link: report.reference_script.license.url,
             license_name: report.reference_script.license.name,
             tldr_legal_link: It.link(report.reference_script.license.explanation_url, target: 'tldrlegal'))
      %>
    <% elsif report.reference_script.license || report.reference_script.license_text %>
      <%= it('reports.report_intro_unauthorized_license_no_url',
             other_script: report.reference_script ? render_script(report.reference_script) : link_to(report.script_url, report.script_url, rel: :nofollow),
             license_name: report.reference_script.license&.name || report.reference_script.license_text,
             tldr_legal_link: It.link(report.reference_script.license ? report.reference_script.license.explanation_url : License.tldr_search_url(report.reference_script.license_text), target: 'tldrlegal'))
      %>
    <% else %>
      <%= it('reports.report_intro_unauthorized_no_license',
             other_script: report.reference_script ? render_script(report.reference_script) : link_to(report.script_url, report.script_url, rel: :nofollow))
      %>
    <% end %>
  </p>
<% end %>

<% if current_user&.moderator? && report.awaiting_response? && !report.resolved? %>
  <p><b>(Awaiting response from author)</b></p>
<% end %>

<% case report.item %>
<% when Comment %>
  <div><%= t('common.quote.prefix_html', user: render_user(report.reported_users.first, report.reported_user_id)) %></div>
  <blockquote>
    <div class="user-content">
      <%= format_user_text(report.item.text, report.item.text_markup) %>
      <%= render partial: 'screenshots/show', locals: { attachable: report.item } %>
    </div>
  </blockquote>
<% when Discussion %>
  <% if report.item.first_comment %>
    <div><%= t('common.quote.prefix_html', user: render_user(report.reported_users.first, report.reported_user_id)) %></div>
    <blockquote>
      <div class="user-content">
        <%= format_user_text(report.item.first_comment.text, report.item.first_comment.text_markup) %>
        <%= render partial: 'screenshots/show', locals: { attachable: report.item.first_comment } %>
      </div>
    </blockquote>
  <% end %>
<% when Message %>
  <div><%= t('common.quote.prefix_html', user: render_user(report.reported_users.first, report.reported_user_id)) %></div>
  <blockquote>
    <div class="user-content">
      <%= format_user_text(report.item.content, report.item.content_markup) %>
      <%= render partial: 'screenshots/show', locals: { attachable: report.item } %>
    </div>
  </blockquote>
<% end %>

<% if report.explanation.present? %>
  <% if show_reporter %>
    <div><%= t('common.quote.prefix_html', user: report.auto_reporter || render_user(report.reporter, report.reporter_id)) %></div>
  <% else %>
    <div><%= t('common.quote.anonymous_reporter_html') %></div>
  <% end %>
  <blockquote>
    <div class="user-content">
      <%= format_user_text(report.explanation, report.explanation_markup) %>
      <%= render partial: 'screenshots/show', locals: { attachable: report } %>
    </div>
  </blockquote>
<% end %>

<% if report.private_explanation.present? && current_user&.moderator? %>
  <div>Automated checks report:</div>
  <blockquote>
    <div class="user-content">
      <%= format_user_text(report.private_explanation, 'markdown') %>
    </div>
  </blockquote>
<% end %>


<% if report.rebuttal.present? %>
  <div><%= t('common.quote.prefix_html', user: render_user(report.rebuttal_by_user, report.rebuttal_by_user_id, script: (report.item&.is_a?(Script) ? report.item : nil))) %></div>
  <blockquote>
    <div class="user-content">
      <%= format_user_text(report.rebuttal, 'text') %>
    </div>
  </blockquote>
<% end %>

<% if report.unauthorized_code? && report.reference_script %>
  <table class="unauthorized-code-comparison">
    <thead>
    <tr>
      <th></th>
      <th><%= t('reports.unauthorized_code.script_header') %></th>
      <th><%= t('scripts.author_name') %></th>
      <th><%= t('scripts.license') %></th>
      <th><%= t('scripts.created_date') %></th>
      <th><%= t('scripts.updated_date') %></th>
    </tr>
    </thead>
    <tbody>
    <tr>
      <td><%= t('reports.unauthorized_code.reported_script') %></td>
      <td><%= render_script(report.item) %></td>
      <td><%= report.item.users.map { |u| link_to(u.name, u) }.to_sentence.html_safe %></td>
      <td><%= license_display(report.item) %></td>
      <td><%= markup_date(report.item.created_at) %></td>
      <td><%= markup_date(report.item.code_updated_at) %></td>
    </tr>
    <tr>
      <td><%= t('reports.unauthorized_code.original_script') %></td>
      <% if report.reference_script %>
        <td><%= render_script(report.reference_script) %></td>
        <td><%= report.reference_script.users.map { |u| link_to(u.name, u) }.to_sentence.html_safe %></td>
        <td><%= license_display(report.reference_script) %></td>
        <td><%= markup_date(report.reference_script.created_at) %></td>
        <td><%= markup_date(report.reference_script.code_updated_at) %></td>
      <% else %>
        <td><%= link_to report.script_url %></td>
      <% end %>
    </tr>
    </tbody>
  </table>

  <% if report.reference_script %>
    <div class="report-diff">
      <%= button_tag t('reports.load_diff'), class: 'lazy-load-diff', data: { url: diff_report_path(report), disable_with: t('reports.loading_diff') } %>
      <% if report.reference_script.cleaned_code %>
        <%= button_tag t('reports.load_cleaned_diff'), class: 'lazy-load-diff', data: { url: diff_report_path(report, tersed: 1), disable_with: t('reports.loading_diff') } %>
      <% end %>
    </div>
  <% end %>
<% end %>

<% if report.item.is_a?(Script) && report.item.code_updated_at > report.created_at %>
  <p><%= t('reports.script_updated_since_report') %></p>
<% end %>

<% if report.item.is_a?(Script) %>
  <% previous_report_count = report.item.reports.where(result: [Report::RESULT_FIXED, Report::RESULT_UPHELD]).where.not(id: report.id).count %>
  <% if previous_report_count > 0 %>
    <p><%= link_to t('reports.script_previous_reports', count: previous_report_count), reports_path(script_id: report.item_id, non_dismissed: '1') %></p>
  <% end %>
<% end %>

<% if current_user&.moderator? %>
  <% ror = report.recent_other_reports.load %>
  <% if ror.any? %>
    <p><%= report_item_link(report) %> has been recently reported:</p>
    <ul>
      <% ror.each do |other_report| %>
        <li>
          <%= link_to report_url(other_report) do %>Report #<%= other_report.id %> by <%= other_report.auto_reporter || render_user(other_report.reporter, other_report.reporter_id, skip_link: true) %> for <%= other_report.reason_text %><% end %>
          - <%= other_report.result || 'pending' %> <%= "(for #{other_report.upheld_reason_text})" if other_report.moderator_reason_override %>
          <% if other_report.script_lock_appeals.any? %>
            <ul>
              <% other_report.script_lock_appeals.each do |sla| %>
                <li>
                  <%= t('reports.other_appeals.item_html', item_link: link_to(t('reports.other_appeals.item_link', id: sla.id), script_script_lock_appeal_path(sla.script, sla)), result: t("reports.other_appeals.resolutions.#{sla.resolution}")) %>
                </li>
              <% end %>
            </ul>
          <% end %>
        </li>
      <% end %>
    </ul>
  <% end %>

  <% if report.reporter %>
    <%= render partial: 'reports/reporter_stats', locals: { reporter: report.reporter, ignore_report: report} %>
  <% end %>
<% end %>

<% report.reported_users.each do |reported_user| %>
  <p>
    <%= t('reports.reported_user_activity_html', user: render_user(reported_user, reported_user.id)) %>
  </p>
  <ul>
    <li>
      <%= link_to t('activerecord.models.script', count: reported_user.scripts.not_deleted.count), user_path(reported_user.id, anchor: 'user-script-list') %>
      <% if current_user&.moderator? %>
        <% if (locked_count = reported_user.scripts.locked.count) > 0 %>
          (and <%= locked_count %> locked <%= 'script'.pluralize(locked_count) %>)
        <% end %>
      <% end %>
    </li>
    <li>
      <%= link_to t('activerecord.models.comment', count: reported_user.comments.not_deleted.count), discussions_path(user: reported_user.id, visibility: current_user&.moderator? ? 'all' : nil) %>
      <% if current_user&.moderator? %>
        <% if (locked_count = reported_user.discussions.soft_deleted.count) > 0 %>
          (and <%= locked_count %> deleted <%= 'discussion'.pluralize(locked_count) %>)
        <% end %>
      <% end %>
    </li>
  </ul>
<% end %>

<% if report.discussions.any? %>
  <p>
    <b><%= link_to t('reports.view_discussion'), report.discussions.first.path(locale: request_locale.code) %></b>
  </p>
<% elsif current_user&.moderator? %>
  <p>
    <%= link_to('Start a discussion', new_discussion_path(report_id: report.id)) %>
  </p>
<% end %>

<% if report.upheld? %>
  <p>
    <% if report.self_upheld %>
      <%= t('reports.result_self_upheld') %>
    <% elsif current_user&.moderator? && report.resolver %>
      <% if report.moderator_reason_override %>
        This report has been upheld by <%= render_user(report.resolver, report.resolver_id) %>, but they upheld it as <%= report.upheld_reason_text %>.
      <% else %>
        This report has been upheld by <%= render_user(report.resolver, report.resolver_id) %>.
      <% end %>
    <% elsif current_user&.moderator? && report.automod_resolved %>
      This report has been upheld automatically.
    <% else %>
      <% if report.moderator_reason_override %>
        <%= t('reports.result_upheld_reason_override', reason: report.upheld_reason_text) %>
      <% else %>
        <%= t('reports.result_upheld') %>
      <% end %>
    <% end %>
  </p>
  <% if report.moderator_notes.present? %>
    <blockquote>
      <div class="user-content">
        <%= format_user_text(report.moderator_notes, 'text') %>
      </div>
    </blockquote>
  <% end %>
  <% if current_user && report.item.is_a?(Script) && report.item.users.include?(current_user) %>
    <p>You can <%= link_to('submit an appeal', new_script_script_lock_appeal_path(report.item, report_id: report.id)) %> to get your script undeleted.</p>
  <% end %>
<% elsif report.fixed? %>
  <p>
    <% if current_user&.moderator? && report.resolver %>
      This report has been marked as fixed by <%= render_user(report.resolver, report.resolver_id) %>.
    <% else %>
      <%= t('reports.result_fixed') %>
    <% end %>
  </p>
  <% if report.moderator_notes.present? %>
    <blockquote>
      <div class="user-content">
        <%= format_user_text(report.moderator_notes, 'text') %>
      </div>
    </blockquote>
  <% end %>
<% elsif report.dismissed? %>
  <p>
    <% if current_user&.moderator? && report.resolver %>
      This report has been dismissed by <%= render_user(report.resolver, report.resolver_id) %>.
    <% else %>
      <%= t('reports.result_dismissed') %>
    <% end %>
  </p>
  <% if report.moderator_notes.present? %>
    <blockquote>
      <div class="user-content">
        <%= format_user_text(report.moderator_notes, 'text') %>
      </div>
    </blockquote>
  <% end %>
<% elsif current_user && report.item.is_a?(Script) && report.item.users.include?(current_user) %>
  <p>
    <%= t('reports.result_pending_author_html') %>
  </p>
  <% unless report.rebuttal.present? %>
    <%= form_with(model: report, url: rebut_report_path(report), method: 'POST', class: 'report-resolution-options') do |f| %>
      <%= f.text_area :rebuttal, required: true, placeholder: t('reports.rebuttal_placeholder'), maxlength: 10_000, rows: 6 %>
      <br>
      <%= f.submit t('reports.submit_rebuttal') %>
    <% end %>
  <% end %>
  <%= form_tag(uphold_report_path(report), method: 'POST', class: 'report-resolution-options') do %>
    <%= submit_tag(t('reports.delete_script')) %>
  <% end %>
<% elsif current_user&.moderator? %>
  <%= form_tag(dismiss_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 'Dismiss report' %>
  <% end %>
  <% if report.resolvable_by_moderator?(current_user) %>
    <%= render partial: 'reports/moderator_controls', locals: { report: report, i: i } %>
  <% elsif report.admin_only? %>
    <p class="report-resolution-options">(This report can only be resolved by admin.)</p>
  <% else %>
    <p class="report-resolution-options">(You're involved in this report, you can't resolve it yourself.)</p>
  <% end %>
<% else %>
  <p>
    <%= t('reports.result_pending') %>
  </p>
<% end %>
