import Users from "meteor/vulcan:users";
import { addCallback } from 'meteor/vulcan:core';
import { Posts } from '../posts'

const MODERATE_OWN_PERSONAL_THRESHOLD = 50
const TRUSTLEVEL1_THRESHOLD = 2000
import { addEditableCallbacks } from '../../../server/editor/make_editable_callbacks.js'
import { makeEditableOptionsModeration } from './custom_fields.js'

function updateTrustedStatus ({newDocument, vote}) {

  const user = Users.findOne(newDocument.userId)
  if (user.karma >= TRUSTLEVEL1_THRESHOLD && (!Users.getGroups(user).includes('trustLevel1'))) {
    Users.update(user._id, {$push: {groups: 'trustLevel1'}});
    const updatedUser = Users.findOne(newDocument.userId)
    //eslint-disable-next-line no-console
    console.info("User gained trusted status", updatedUser.username, updatedUser._id, updatedUser.karma, updatedUser.groups)
  }
}

addCallback("votes.smallUpvote.async", updateTrustedStatus);
addCallback("votes.bigUpvote.async", updateTrustedStatus);

function updateModerateOwnPersonal({newDocument, vote}) {
  const user = Users.findOne(newDocument.userId)
  if (user.karma >= MODERATE_OWN_PERSONAL_THRESHOLD && (!Users.getGroups(user).includes('canModeratePersonal'))) {
    Users.update(user._id, {$push: {groups: 'canModeratePersonal'}});
    const updatedUser = Users.findOne(newDocument.userId)
    //eslint-disable-next-line no-console
    console.info("User gained trusted status", updatedUser.username, updatedUser._id, updatedUser.karma, updatedUser.groups)
  }
}

addCallback("votes.smallUpvote.async", updateModerateOwnPersonal);
addCallback("votes.bigUpvote.async", updateModerateOwnPersonal);

function maybeSendVerificationEmail (modifier, user)
{
  if(modifier.$set.whenConfirmationEmailSent
      && (!user.whenConfirmationEmailSent
          || user.whenConfirmationEmailSent.getTime() !== modifier.$set.whenConfirmationEmailSent.getTime()))
  {
    Accounts.sendVerificationEmail(user._id);
  }
}

addCallback("users.edit.sync", maybeSendVerificationEmail);

addEditableCallbacks({collection: Users, options: makeEditableOptionsModeration})




function approveUnreviewedPosts (newUser, oldUser)
{
  if(newUser.reviewedByUserId && !oldUser.reviewedByUserId)
  {
    Posts.update({userId:newUser._id, authorIsUnreviewed:true}, {$set:{authorIsUnreviewed:false, postedAt: new Date()}})
  }
}

addCallback("users.edit.async", approveUnreviewedPosts);

// When the very first user account is being created, add them to Sunshine
// Regiment. Patterned after a similar callback in
// vulcan-users/lib/server/callbacks.js which makes the first user an admin.
function makeFirstUserAdminAndApproved (user) {
  const realUsersCount = Users.find({'isDummy': {$in: [false,null]}}).count();
  if (realUsersCount === 0) {
    user.reviewedByUserId = "firstAccount"; //HACK
    
    // Add the first user to the Sunshine Regiment
    if (!user.groups) user.groups = [];
    user.groups.push("sunshineRegiment");
  }
  return user;
}

addCallback('users.new.sync', makeFirstUserAdminAndApproved);

@@ -838,33 +838,32 @@
 r.groups)%0A  %7D%0A%7D%0A
-%0A
 addCallback(%22vot
@@ -1482,33 +1482,32 @@
 r.groups)%0A  %7D%0A%7D%0A
-%0A
 addCallback(%22vot
@@ -1927,25 +1927,24 @@
 _id);%0A  %7D%0A%7D%0A
-%0A
 addCallback(
@@ -2075,19 +2075,16 @@
 tion%7D)%0A%0A
-%0A%0A%0A
 function
@@ -2319,17 +2319,16 @@
 )%0A  %7D%0A%7D%0A
-%0A
 addCallb
@@ -2947,17 +2947,16 @@
 user;%0A%7D%0A
-%0A
 addCallb
@@ -3005,12 +3005,510 @@
 dApproved);%0A
+%0Afunction clearKarmaChangeBatchOnSettingsChange (modifier, user)%0A%7B%0A  if (modifier.$set && modifier.$set.karmaChangeNotifierSettings) %7B%0A    if (!user.karmaChangeNotifierSettings.updateFrequency%0A      %7C%7C modifier.$set.karmaChangeNotifierSettings.updateFrequency !== user.karmaChangeNotifierSettings.updateFrequency) %7B%0A      modifier.$set.karmaChangeLastOpened = null;%0A      modifier.$set.karmaChangeBatchStart = null;%0A    %7D%0A  %7D%0A%7D%0AaddCallback(%22users.edit.sync%22, clearKarmaChangeBatchOnSettingsChange);

Clear karma-change notifier last-batch on settings change