<div class="row">
  <div class="col-12">
    <h2 class="d-inline">
      <%= @user.name %><%= link_to "@#{@user.username}", @user.path, class: "ml-2", target: "_blank", rel: "noopener" %>
    </h2>
    <a href="<%= internal_user_path(@user) %>" class="btn btn-primary float-right">Internal Profile</a>
    <p class="font-italic">Member since <%= @user.created_at.strftime("%b %e '%y") %></p>
  </div>
</div>

<%= render "activity" %>

<div class="row">
  <div class="col-12">
    <h2 class="d-inline">User Status</h2>
    <h3 class="float-right">
      <% if @user.banned %>
        <span class="badge badge-danger">🚨 Member is Suspended 🚨</span>
      <% elsif @user.warned %>
        <span class="badge badge-warning">Member is Warned</span>
      <% elsif @user.comment_banned %>
        <span class="badge badge-warning">Member is Comment Banned</span>
      <% elsif @user.trusted %>
        <span class="badge badge-success">Member is Trusted</span>
      <% else %>
        <span class="badge badge-info">Member is in Good Standing</span>
      <% end %>
    </h3>
    <%= form_for(@user, url: user_status_internal_user_path(@user), html: { class: "mt-3" }) do |f| %>
      <div class="form-group">
        <%= f.label "Select new user status", class: "mr-3" %>
        <%= f.select(:user_status, options_for_select(["Warn", "Comment Ban", "Ban", "Regular Member", "Trusted", "Pro"]), include_blank: true) %>
      </div>
      <div class="form-group">
        <%= f.label "Reason for action:" %>
        <%= f.text_area :note_for_current_role, required: true, class: "form-control" %>
      </div>
      <%= f.submit "Update User Status", class: "btn btn-primary float-right" %>
    <% end %>
  </div>
</div>

<%= render "notes" %>

<div class="row my-3">
  <div class="col-12">
    <h2 class="d-inline">Remove Identity</h2>
    <button type="button" data-toggle="collapse" data-target="#identity-row" class="btn btn-secondary float-right">Toggle</button>
  </div>
  <div class="col-12 collapse" id="identity-row">
    <p>Removing a social account identity can solve certain sign in issues, for example:</p>
    <ul>
      <li>
        They created an account on <%= community_name %>, but deleted their original social account and recreated it with the same Twitter/GitHub username.
        This will be true if their Twitter/GitHub account's UID does not match their identity's UID. You can use the following third party tools to check:
        <ul>
          <li>
            <a href="https://tweeterid.com/?username=<%= @user.twitter_username %>" target="_blank" rel="noopener nofollow">Tweeter ID for Twitter (username is in URL for reference)</a>
          </li>
          <li>
            <a href="https://caius.github.io/github_id/#<%= @user.github_username %>" target="_blank" rel="noopener nofollow">Caius's GitHub ID Lookup Tool</a>
          </li>
        </ul>
        Steps to check:
        <ol>
          <li>Click one of the links to check their social account's UID.</li>
          <li>Confirm whether or not it matches with the identity's UID.</li>
          <li>If it doesn't match, delete the respective identity.</li>
          <li>Ask the user to reauthorize their social account via <%= app_url(user_settings_path(:account)) %></li>
        </ol>
      </li>
      <% @user.identities.each do |identity| %>
        <%= form_for(@user, url: remove_identity_internal_user_path(@user), html: { method: :delete, onsubmit: "return confirm('Are you sure? This should only be done as a solution for the listed example(s).)" }) do |f| %>
          <%= f.hidden_field :identity_id, value: identity.id %>
          <p><b><%= identity.provider.capitalize %> UID: <%= identity.uid %> - Username: <%= identity.auth_data_dump.present? ? identity.auth_data_dump["info"]["nickname"] : "no auth data dump available! 😞" %></b></p>
          <%= f.submit "Delete #{identity.provider.capitalize} Identity", class: "btn btn-danger" %>
        <% end %>
      <% end %>
      <h3>This should be done only do this if you are certain the user is having the specific problem(s) listed above.</h3>
      <% if @user.backup_data.any? %>
        <hr>
        <h2>Recover a deleted identity:</h2>
        <ul>
          <% @user.backup_data.where(instance_type: "Identity").each do |data| %>
            <li>
              <%= "#{data.json_data['provider'].capitalize} #{data.instance_type} - UID: #{data.json_data['uid']}" %>
              <%= form_for(@user, url: recover_identity_internal_user_path(@user), html: { method: :post, onsubmit: "return confirm('Please confirm you want to recover the #{data.json_data['provider'].capitalize} identity.')" }) do |f| %>
                <%= f.hidden_field :backup_data_id, value: data.id %>
                <%= f.submit "Recover #{data.json_data['provider'].capitalize} Identity" %>
              <% end %>
            </li>
          <% end %>
        </ul>
      <% end %>
    </ul>
  </div>
</div>

<div class="row">
  <div class="col-12 mb-5">
    <h2 class="d-inline">Destructive Actions</h2>
    <button type="button" data-toggle="collapse" data-target="#destructive-actions" class="btn btn-secondary float-right">Toggle</button>
  </div>
</div>

<div id="destructive-actions" class="row collapse">
  <div class="col-12">
    <h3>Merge User</h3>
    <p>To merge a duplicate account, make sure you are currently on the page of the user you want to KEEP. Below, add the user id of the account to merge information from, and delete.</p>
    <%= form_for(@user, url: merge_internal_user_path(@user), html: { method: :post, onsubmit: "return confirm('Are you sure? This is extremely destructive and irreversible. Merging will delete all the other users content and combine it with this user')" }) do |f| %>
      <%= f.label "User to be deleted:" %>
      <%= f.text_field :merge_user_id, placeholder: "#ID" %>
      <%= f.submit "Merge Users", class: "btn btn-danger float-right" %>
    <% end %>
  </div>

  <div class="col-12 my-2">
    <h3>Banish User</h3>
    <% if @user.comments.where("created_at < ?", 100.days.ago).empty? && @user.created_at < 100.days.ago %>
      <p>
        <em>This is extremely destructive. Banishing will delete all the user's existing content and change their username to @spam_###.</em>
      </p>
      <p><strong>Do not do this lightly.</strong></p>
      <%= form_for(@user, url: banish_internal_user_path(@user), html: { method: :post, onsubmit: "return confirm('Are you sure? This is extremely destructive and irreversible. Banishing will delete all articles and turn their username into @spam_###')" }) do %>
        <button class="btn btn-danger float-right">🚫 Banish User for Spam 🚫</button>
      <% end %>
    <% elsif current_user.has_role?(:super_admin) %>
      <p><strong>This is not a new user.</strong> You are only allowed to take this action because you are a
        <strong>super admin.</strong></p>
      <p>
        <em>This is extremely destructive. Banishing will delete all the user's existing content and change their username to @spam_###.</em>
      </p>
      <p><strong>Do not do this lightly.</strong></p>
      <%= form_for(@user, url: banish_internal_user_path(@user), html: { method: :post, onsubmit: "return confirm('Are you sure? This is extremely destructive and irreversible. Banishing will delete all articles and turn their username into @spam_###')" }) do %>
        <button class="btn btn-danger float-right">Banish User for Spam!</button>
      <% end %>
    <% else %>
      <p><em>This is not a new user so you'll need to contact a super admin to banish this user.</em></p>
    <% end %>
  </div>

  <% if current_user.has_role?(:super_admin) %>
    <div class="col-12 my-2">
      <h3>Delete User & Turn Content Into Ghost</h3>
      <p>This will
        <strong>completely destroy the user</strong> and convert all of their articles & comments into a ghost author. This action is irreversible.
      </p>
      <p><strong>Do not do this lightly.</strong></p>
      <%= form_for(@user, url: full_delete_internal_user_path(@user), html: { method: :post, onsubmit: "return confirm('Are you sure? 👻 This is extremely destructive and irreversible. The user will be deleted and their articles & comments will be converted to Ghost')" }) do |f| %>
        <%= f.hidden_field :ghostify, value: true %>
        <button class="btn btn-danger float-right">👻 Fully Delete User & Ghostify 👻</button>
      <% end %>
    </div>

    <div class="col-12 my-2">
      <h3>Fully Delete User</h3>
      <p>This will
        <strong>completely destroy the user</strong> and all of their activity from our database. This action is irreversible.
      </p>
      <p><strong>Do not do this lightly.</strong></p>
      <%= form_for(@user, url: full_delete_internal_user_path(@user), html: { method: :post, onsubmit: "return confirm('Are you sure? This is extremely destructive and irreversible.')" }) do |f| %>
        <%= f.hidden_field :ghostify, value: false %>
        <button class="btn btn-danger float-right">☠️ Fully Delete User & All Activity ☠️</button>
      <% end %>
    </div>
  <% end %>
</div>
