<%- title @user.name %>
<%- description "Greasy Fork profile for #{@user.name}" %>

<section id="about-user" class="text-content reportable">
  <% if @ad_method&.ea? %>
    <%= render(partial: 'shared/ea_text_ad', locals: {ad_method: @ad_method, ea_div_id: 'user-show-ea'}) %>
  <% elsif @ad_method&.ga? %>
    <%= render(partial: 'shared/ea_fallback', locals: { div_id: 'user-show-ad-fallback', ea_div_id: 'user-show-ea'})%>
  <% end %>

  <%= link_to(t('reports.report_user'), new_report_path(item_class: 'user', item_id: @user.id), class: 'report-link report-link-abs') unless @user.banned? %>

  <h2><%= render_user(@user, @user.id, skip_link: true) %></h2>

  <%= render partial: 'shared/in_page_ad', locals: { ad_method: @ad_method, div_id: 'user-show-ad', adsense_slot: '4899539664', skip_ea: true} %>

  <% if @user.profile.present? && (current_user&.moderator? || @show_profile) %>
    <section id="user-profile" class="user-content">
      <%= format_user_text(@user.profile, @user.profile_markup, mentions: @user.mentions) %>
    </section>
  <% end %>

  <% if current_user && !@same_user %>
    <p>
      <% conversation = current_user.existing_conversation_with([@user]) %>
      <% if conversation %>
        <%= link_to t('conversations.send_message'), user_conversation_path(current_user, conversation) %>
      <% else %>
        <%= link_to t('conversations.send_message'), new_user_conversation_path(current_user, other_user: @user.name) %>
      <% end %>
    </p>
  <% end %>

  <% if @same_user %>
    <section id="control-panel">
      <header>
        <h3><%= t('users.control_panel') %></h3>
      </header>
      <ul id="user-control-panel">
        <li><%= link_to t('scripts.new'), new_script_version_path %></li>
        <li><%= link_to t('scripts.new_css'), new_script_version_path(language: 'css') %></li>
        <li><%= link_to t('script_sets.new'), new_user_script_set_path(@user) %></li>
        <li><%= link_to t('scripts.import'), import_start_path %></li>
        <li><%= link_to t('users.set_up_webhook'), user_webhook_info_path %></li>
        <li><%= link_to t('users.edit'), edit_user_registration_path %></li>
        <li><%= link_to t('users.edit_sign_in'), user_edit_sign_in_path %></li>
        <li>
          <%= link_to notifications_path(@user) do %>
            <%= t('users.view_notifications') %>
            <% if notification_widget_count %>
              <span class="notification-widget"><%= notification_widget_count %></span>
            <% end %>
          <% end %>
        </li>
        <li><%= link_to t('users.edit_notifications'), notification_settings_user_path(@user) %></li>
        <li><%= link_to t('users.delete_link'), user_delete_info_path %></li>
        <li><%= link_to t('users.sign_out'), destroy_user_session_path, method: :delete %></li>
      </ul>
    </section>
  <% end %>
</section>

<% if @same_user && @user.scripts.any?
  dosr = @user.discussions_on_scripts_written.visible.includes(:poster, :stat_last_replier, :stat_first_comment, script: [:localized_attributes, :users]) %>
  <section id="user-discussions-on-scripts-written">
    <header>
      <h3><%= t('users.discussions_on_scripts') %></h3>
    </header>
    <section class="text-content">
      <% dosr = dosr.limit(11).load %>
      <% if dosr.empty? %>
        <p><%= t('users.discussions_on_scripts_none') %></p>
      <% else %>
        <%= render collection: dosr.first(10), partial: 'discussions/discussion', locals: { show_script: true } %>
        <% if dosr.length > 10 %>
          <%= link_to t('users.discussions_on_scripts_more'), discussions_path(me: 'script') %>
        <% end %>
      <% end %>
    </section>
  </section>
<% end %>

<% if current_user&.moderator? && !@user.moderator? %>
  <section id="moderator-user-control-panel">
    <header>
      <h3>Moderator control panel</h3>
    </header>
    <section class="text-content">
      <p>
        <% if @user.banned? %>
          <% if @user.non_locked_scripts.any? %>
            User was banned <%= markup_date(@user.banned_at) %>, but has non-locked scripts. <%= link_to 'Delete all', user_ban_path(@user) %>
          <% else %>
            User was banned <%= markup_date(@user.banned_at) %>.
          <% end %>
          <%= link_to 'Unban', user_unban_path(@user) %>
        <% elsif @user.bannable? %>
          <%= link_to 'Ban', user_ban_path(@user) %> |
          <%= link_to 'Ban as spammer', user_do_ban_path(@user, reason: Report::REASON_SPAM, delete_type: 'keep', delete_comments: 1), data: { turbo: true, turbo_method: :POST, turbo_confirm: "Ban '#{@user.name}' as a spammer, deleting all scripts and comments?" } %>
        <% end %>
      </p>
      <p>
        <% if @user.email_domain %>
          Email domain: <%= link_to @user.email_domain, users_path(email_domain: @user.email_domain) %><br>
        <% end %>
        <% if @user.unconfirmed_email %>
          <%= link_to "Confirm email change to #{redacted_email(@user.unconfirmed_email)}", mark_email_as_confirmed_user_path(@user), data: { turbo: true, turbo_method: :PATCH } %><br>
        <% elsif !@user.confirmed? %>
          <%= link_to 'Mark email as confirmed', mark_email_as_confirmed_user_path(@user), data: { turbo: true, turbo_method: :PATCH } %><br>
        <% end %>
        <%= link_to 'Users with the same IP', users_path(same_ip: @user.id) %>
      </p>
    </section>
  </section>

  <% all_reports = @user.reports_as_reporter.order(id: :desc) %>
  <% if all_reports.any? %>
    <section>
      <header>
        <h3>Recent reports</h3>
      </header>
      <section class="text-content">
        <%= render partial: 'reports/reporter_stats', locals: { reporter: @user } %>
        <% recent_reports = all_reports.where(created_at: 1.week.ago..).limit(5) %>
        <% if recent_reports.any? %>
          <ul>
            <% recent_reports.each do |report| %>
              <li>
                <%= render partial: 'reports/report', locals: { report: report } %>
              </li>
            <% end %>
          </ul>
        <% end %>
        <% if all_reports.count > recent_reports.count %>
          <p>
            <%= link_to "See all #{all_reports.count} reports.", reports_path(user_id: @user.id) %>
          </p>
        <% end %>
      </section>
    </section>
  <% end %>
<% end %>

<section id="user-discussions">
  <header>
    <h3>
      <%= t('discussions.user_activity.title') %>
    </h3>
  </header>
  <section class="text-content">
    <% recent_comments = @user.comments.not_deleted.joins(:discussion).merge(Discussion.visible).where(created_at: 1.month.ago..).includes(discussion: [:script, :discussion_category]).order(created_at: :desc).limit(10).load %>
    <% if recent_comments.any? %>
      <ul>
        <% recent_comments.each do |comment| %>
          <li><%= link_to user_activity_title(comment.discussion, post: comment.first_comment?), comment.path %></li>
        <% end %>
      </ul>
    <% else %>
      <p>
        <%= t('discussions.user_activity.no_recent_activity') %>
      </p>
    <% end %>
    <% if current_user&.moderator? %>
      <% if @user.comments.any? %>
        <p>
          <%= link_to t('discussions.user_activity.see_all_activity'), discussions_path(user: @user.id, visibility: 'all') %>
        </p>
      <% end %>
    <% elsif @user.comments.not_deleted.any? %>
      <p>
        <%= link_to t('discussions.user_activity.see_all_activity'), discussions_path(user: @user.id) %>
      </p>
    <% end %>
  </section>
</section>

<% if @same_user %>
  <section id="user-conversations">
    <header>
      <h3><%= t('conversations.recent.title') %></h3>
    </header>
    <section class="text-content">
      <% if @recent_conversations.any? %>
        <ul>
          <% @recent_conversations.each do |conversation| %>
            <li>
              <% other_users = (conversation.users - [@user]) %>
              <%= link_to t('conversations.conversation_link_html', users: other_users.map { |u| render_user(u, u.id, skip_link: true) }.to_sentence.html_safe, count: other_users.count), user_conversation_path(@user, conversation) %>
              <%= t('conversations.conversation_info_html', date: markup_date(conversation.stat_last_message_date), user: render_user(conversation.stat_last_poster, conversation.stat_last_poster_id, skip_link: true)) %>
            </li>
          <% end %>
        </ul>
        <% if @more_conversations %>
          <p><%= link_to t('conversations.recent.more'), user_conversations_path(@user) %></p>
        <% end %>
      <% elsif @more_conversations %>
        <p>
          <%= t('conversations.no_recent_conversations') %>
          <%= link_to t('conversations.recent.more'), user_conversations_path(@user) %>
        </p>
      <% else %>
        <p>
          <%= t('conversations.no_conversations') %>
        </p>
      <% end %>
      <p><%= link_to t('conversations.new_link'), new_user_conversation_path(@user) %></p>
    </section>
  </section>
<% end %>

<% script_sets = @user.script_sets.order(:name) %>
<% if @same_user || ((current_user&.moderator? || @show_profile) && script_sets.load.any?) %>
  <section id="user-script-sets-section">
    <header>
      <h3><%= t('script_sets.heading') %></h3>
    </header>
    <section class="text-content">
      <ul id="user-script-sets">
        <% favorites_set = script_sets.find(&:favorite)
        if !favorites_set.nil? %>
          <li><%= link_to t('script_sets.favorites_name'), scripts_path(set: favorites_set.id), rel: :nofollow %><% if @same_user %> - <%= link_to t('script_sets.edit_link'), edit_user_script_set_path(@user, favorites_set) %><% end %></li>
        <% elsif @same_user %>
          <li><%= t('script_sets.favorites_name') %> - <%= link_to t('script_sets.edit_link'), new_user_script_set_path(@user, fav: 1) %></li>
        <% end
         script_sets.reject(&:favorite).each do |ss| %>
          <li><%= link_to ss.name, scripts_path(set: ss.id), rel: :nofollow %>: <%= ss.description %><% if @same_user %> - <%= link_to t('script_sets.edit_link'), edit_user_script_set_path(@user, ss) %><% end %></li>
        <% end %>
      </ul>
    </section>
  </section>
<% end %>

<div class="sidebarred">
  <div class="sidebarred-main-content">
    <% if @user_has_scripts %>
      <div class="open-sidebar sidebar-collapsed">
        ☰
      </div>
    <% end %>

    <section id="user-script-list-section">
      <header>
        <h3><%= t('users.script_list') %></h3>
      </header>
      <% if !@user_has_scripts
        if @same_user %>
          <p><%= t('users.script_list_none_same_user_html', script_list_none_same_user_info_link: link_to(t('users.script_list_none_same_user_info_link_text'), help_writing_user_scripts_path)) %></p>
        <% else %>
          <p><%= t('users.script_list_none') %></p>
        <% end
      elsif @scripts.empty? %>
        <p><%= t('users.script_list_none_for_filter') %></p>
      <% else %>
        <ol id="user-script-list" class="script-list <%= 'showing-all-languages' if params[:language].nil? %>">
          <%= render partial: 'scripts/script', collection: @scripts, locals: {show_author_details: true}, cached: (params[:locale_override].nil? && greasy?) ? ->(script) { ['user', script, I18n.locale] } : false %>
        </ol>
      <% end %>

      <%= render_pagination %>
    </section>

    <% unless @unlisted_scripts.empty? %>
      <section id="user-unlisted-script-list-section">
        <header>
          <h3><%= t('users.unlisted_script_list') %></h3>
        </header>
        <ol id="user-unlisted-script-list" class="script-list">
          <%= render partial: 'scripts/script', collection: @unlisted_scripts %>
        </ol>
      </section>
    <% end %>

    <% unless @libraries.empty? %>
      <section id="user-library-list-section">
        <header>
          <h3><%= t('scripts.libraries_title') %></h3>
        </header>
        <ol id="user-library-script-list" class="script-list">
          <%= render partial: 'scripts/script', collection: @libraries %>
        </ol>
      </section>
    <% end %>

    <% unless @deleted_scripts.empty? %>
      <section id="user-deleted-script-list-section">
        <header>
          <h3><%= t('users.deleted_script_list') %></h3>
        </header>
        <ol id="user-deleted-script-list" class="script-list">
          <%= render partial: 'scripts/script', collection: @deleted_scripts %>
        </ol>
      </section>
    <% end %>

    <% if @other_site_scripts > 0 %>
      <p><%= link_to(t('users.greasy_fork_scripts_link', user_name: @user.name, count: @other_site_scripts), user_url(@user, host: 'greasyfork.org')) %></p>
    <% end %>
  </div>

  <% if @user_has_scripts %>
    <div class="sidebar collapsed">
      <div class="close-sidebar">
        <div class="sidebar-title"><%= t('common.listing_options_title') %></div>
        <div>☰</div>
      </div>
      <%= render partial: 'scripts/list_options', locals: { by_sites: @by_sites, scripts: @scripts, skip_all_sites_link: true, rel: :nofollow } %>
    </div>
  <% end %>
</div>
