const {ObjectId} = require('mongodb');
const jwt = require('jsonwebtoken');

const {Todo} = require('../../models/todo');
const {User} = require('../../models/user');

var bcrypt = require('bcryptjs');

const tools = require('../../tools/general');

let myuserIdOne = '';

const userOneId = new ObjectId();
const userTwoId = new ObjectId();
// const userThreeId = new ObjectId();

// let mypwdcrypt = bcrypt.hash(mypwdchiaro, bcrypt.genSaltSync(12))
//   .then((hashedPassword) => {
//     console.log('pwd=',hashedPassword);
//   });
// String(mypwdcrypt)

let mypwdchiaro = 'mypassword@1A';
let mypwdcrypted = '$2b$12$mteST.isuWO0SNsfeZ0aCe.Dz3qwPh5SU8t9rc5SaPTkb3j0ywGv6';

const date_login = new Date();

// const useragent = "auth Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36"
const useragent = 'node-superagent/2.3.0';

const users = [
  {
    _id: userOneId,
    keyappid: process.env.KEY_APP_ID,
    lang: 'it',
    idapp: '1',
    email: 'paoloa.rena77@gmail.com',
    username: 'paoloar77B',
    name: 'Paolo',
    surname: 'Arena',
    password: mypwdcrypted,
    tokens: [
      {
        access: 'auth',
        browser: useragent,
        token: jwt.sign({_id: userOneId, access: 'auth'}, process.env.SIGNCODE).
            toString(),
        date_login,
      }],
  },
  {
    _id: userTwoId,
    keyappid: process.env.KEY_APP_ID,
    lang: 'it',
    idapp: '1',
    email: 'patesttest@com',
    name: 'Paolo2',
    surname: 'Arena2',
    password: mypwdcrypted,
    username: 'paoloar77C',
    tokens: [
      {
        access: 'auth',
        browser: useragent,
        token: jwt.sign({_id: userTwoId, access: 'auth'}, process.env.SIGNCODE).
            toString(),
        date_login,
      }],
  },
  {
    keyappid: process.env.KEY_APP_ID,      // RECORD CHE VERRA' UTILIZZATO PER UNA NUOVA REGISTRAZIONE
    lang: 'it',
    idapp: '1',
    email: 'pao.loarena77@gmail.com',
    password: mypwdcrypted,
    username: 'paoloar77A',
    name: 'Paolo3',
    surname: 'Arena3',
  }];

const userjson = JSON.stringify(users[0]);

// console.log('userjson=', userjson)

const mynewID = new ObjectId();

const mongoose = require('mongoose').set('debug', false)

const { ObjectId } = require('mongodb');

const todos = [
  {
    _id: mynewID,
    category: new ObjectId(),
    statustodo: 0,
    completed_at: new Date(),
    created_at: new Date(),
    // category: "personal",
    descr: 'Primo Task Esempio',
    enableExpiring: false,
    expiring_at: new Date(),
    modified: false,
    modify_at: new Date(),
    pos: 1,
    priority: 1,
    progress: 0,
    userId: users[0]._id,
  }, {
    _id: new ObjectId(),
    category: new ObjectId(mynewID),
    statustodo: 0,
    completed_at: new Date(),
    created_at: new Date(),
    descr: 'Secondo Task Esempio',
    enableExpiring: false,
    expiring_at: new Date(),
    modified: false,
    modify_at: new Date(),
    pos: 2,
    priority: 1,
    progress: 0,
    userId: users[1]._id,
  }, {
    _id: new ObjectId(),
    category: new ObjectId(mynewID),
    statustodo: 0,
    completed_at: new Date(),
    created_at: new Date(),
    descr: 'Terzo Task Esempio',
    enableExpiring: false,
    expiring_at: new Date(),
    modified: false,
    modify_at: new Date(),
    pos: 3,
    priority: 1,
    progress: 0,
    userId: users[2]._id,
  }, {                       // RECORD CHE VERRA' UTILIZZATO PER AGGIUNGERE UN NUOVO TASK
    _id: new ObjectId(),
    category: new ObjectId(mynewID),
    statustodo: 0,
    completed_at: new Date(),
    created_at: new Date(),
    descr: 'Nuovo Quarto Task Esempio da Inserire',
    enableExpiring: false,
    expiring_at: new Date(),
    modified: false,
    modify_at: new Date(),
    pos: 4,
    priority: 1,
    progress: 0,
    userId: users[0]._id,
  }];

const populateTodos = async (done) => {

  // console.log(' delete Todos...')
  await Todo.deleteMany({}).then(() => {
    // console.log('    deleted ...')
    const TodoOne = new Todo(todos[0]).save();
    const TodoTwo = new Todo(todos[1]).save();

    return Promise.all([TodoOne, TodoTwo]);
  }).then(() => {
    // console.log('populateTodos done !')
    // done()
  });

};

const populateUsers = async (done) => {

  // console.log(' delete Users ...')
  await User.deleteMany({}).then(() => {

    // console.log('    deleted ...')
    // console.log('users[0]', users[0])
    var userOne = new User(users[0]).save();
    var userTwo = new User(users[1]).save();

    return Promise.all([userOne, userTwo]);
  }).then(() => {
    // console.log('populateUsers done !')
    // done()
  });

};

module.exports = {
  todos,
  populateTodos,
  users,
  populateUsers,
  userjson,
  mypwdcrypted,
  mypwdchiaro,
  date_login,
};
