const db = require("../models");
const API = require('../helper/API_Response');
const db_helper = require("../helper/db_helper");
const helper = require("../helper/helper");
const { EmailTemplates, Certificate, User, Cohort } = require("../models");

exports.add = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  res.status(200).send(API._200(await db_helper.addData('EmailTemplates', input)));
};
exports.all = async (req, res) => {
  const all = await EmailTemplates.findAll({
    where: {
      status: 0
    },
    include: [{
      model: Certificate,
      attributes: ['id', 'certificate_name']
    }]
  })
  res.status(200).send(API._200(await all));
};
exports.change_user_email = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  const all = await User.findAll({
    where: {
      email: input.new_email
    }
  })
  if (all.length > 0) {
    res.status(400).send(API._400({ message: "Oops. Looks like email already exist." }));
  } else {
    const result = await db.sequelize.query("update users set email='" + input.new_email + "',is_temp=1,expire_password=1 where email='" + input.old_email + "'; update activity_trackers set email='" + input.new_email + "' where email='" + input.old_email + "'; update authy_masters set email='" + input.new_email + "' where email='" + input.old_email + "'; update failed_emails set email='" + input.new_email + "' where email='" + input.old_email + "'; update selfpaced_class_all_user_schedules set email='" + input.new_email + "' where email='" + input.old_email + "'")
    res.status(200).send(API._200(await result));
  }
};
exports.delete = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  res.status(200).send(API._200(await db_helper.statusChange('EmailTemplates', input.id)));
};
exports.get_payment_transactions = async (req, res) => {
  const result = await db.sequelize.query("select pay.id,pay.user_id,pay.amount,pay.createdAt,pay.product_purchased,pay.tier_essential,pay.name first_name,pay.phone,pay.last_name,pay.is_mini_masterclass_email_sent,u.email,u.aliasname,u.name from payment_transactions pay left join users u on u.id=pay.user_id order by id desc").then((res) => {
    return Array.from(new Set(res));
  })
  res.status(200).send(API._200(await result[0]));
};
exports.getstudentbycohort = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  const all = await Cohort.findAll({
    where: {
      id: input.id,
      status: 0
    }
  });
  let users = [];
  for (let i = 0; i < all.length; i++) {
    let element = all[i].userslist;
    let ids = element.splice(",");
    if (ids.length > 0) {
      for (let j = 0; j < ids.length; j++) {
        let usr = await db_helper.query("select u.id,u.name,u.email,u.aliasname,max(l.createdAt) last_login from users as u left join login_logs as l on l.user_id=u.id  where u.id=" + ids[j]);
        if (usr[0][0].id) {
          users.push({ "id": usr[0][0].id, "name": usr[0][0].name, "email": usr[0][0].email, "aliasname": usr[0][0].aliasname, "last_login": usr[0][0].last_login });
        }
      }
    }
  }
  res.status(200).send(API._200(await users));
};

exports.update = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  res.status(200).send(API._200(await db_helper.update('EmailTemplates', input)));
};