<h1>
  <%= @user.name %><a href="/<%= @user.username %>" target="_blank"> (@<%= @user.username %>)</a>
</h1>
<p>
  <u><a style="color: green" href="/internal/users/<%= @user.id %>">click here view user details & take other actions</a></u>
</p>
<p><em>Member since <%= @user.created_at.strftime("%b %e '%y") %></em></p>
<%= render "internal/users/activity" %>
<div class="row">
  <h2>User Status</h2>
  <% if @user.banned %>
    <p style="background-color:red;color:white">🚨 Member is suspended 🚨</p>
  <% elsif @user.warned %>
    <p style="background-color: orange;color:white">Member is warned</p>
  <% elsif @user.comment_banned %>
    <p style="background-color: orange;color:white">Member is comment banned</p>
  <% elsif @user.trusted %>
    <p style="background-color: green;color:white">Member is trusted</p>
  <% else %>
    <p style="color:green">Member is in good standing</p>
  <% end %>
  <p><strong>Current Roles</strong><em> (view full history in notes below)</em>:</p>
  <ul>
    <% if @user.roles.empty? %>
      <li>Regular Member</li>
    <% end %>
    <% @user.roles.each do |role| %>
        <li><%= role.name %> <em><%= "- " + Tag.find(role.resource_id).name if role.resource_id.present? %></em></li>
    <% end %>
  </ul>
  <%= form_for(@user, url: user_status_internal_user_path(@user)) do |f| %>
    <%= f.label "Select new user status" %>
    <%= f.select(:user_status, options_for_select(["Warn", "Comment Ban", "Ban", "Regular Member", "Trusted", "Pro"]), include_blank: true) %>
    <br>
    <%= f.label "Reason for action:" %>
    <%= f.text_area :note_for_current_role, required: true %>
    <br>
    <%= f.submit "Update User Status" %>
  <% end %>
</div>
<%= render "notes" %>
<header class="internal-edit-header">
  <h2>Remove Identity</h2>
  <button type="button" data-toggle="collapse" data-target="#identity-row">Toggle</button>
</header>
<div class="row 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 dev.to, 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 https://dev.to/settings/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["info"]["nickname"] %></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 class="row">
  <h2>Merge User</h2>
  <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" %>
  <% end %>
</div>
<div class="row">
  <h2>Banish User</h2>
  <% 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" style="font-size:2em;">🚫 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" style="font-size:2em;">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>

<div class="row">
  <h2>Delete User & Turn Content Into Ghost</h2>
  <% if current_user.has_role?(:super_admin) %>
    <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" style="font-size:2em;">👻 Fully Delete User & Ghostify 👻</button>
    <% end %>
  <% else %>
    <p>Only super admins can fully delete users.</p>
  <% end %>
</div>

<div class="row">
  <h2>Fully Delete User</h2>
  <% if current_user.has_role?(:super_admin) %>
    <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" style="font-size:2em;">☠️ Fully Delete User & All Activity ☠️</button>
    <% end %>
  <% else %>
    <p>Only super admins can fully delete users.</p>
  <% end %>
</div>
<br>
<br>
