const db = require("../models");
const API = require('../helper/API_Response');
const db_helper = require("../helper/db_helper");
const helper = require("../helper/helper");
const { LiveEvents, Certificate } = 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('LiveEvents', input)));
};
exports.delete = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  const all_live_events = await db_helper.query("update all_live_events set status=1 where live_event_id=" + input.id);
  const notifications = await db_helper.query("delete from notifications where test_type='meeting' and test_id=" + input.id);
  res.status(200).send(API._200(await db_helper.statusChange('LiveEvents', input.id)));
};
exports.all = async (req, res) => {
  const all = await LiveEvents.findAll({
    where: {
      status: 0
    },
    include: [{
      model: Certificate,
      attributes: ['id', 'certificate_name']
    }]
  })
  res.status(200).send(API._200(await all));
};
exports.byid = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  res.status(200).send(API._200(await db_helper.byId('LiveEvents', input.id)));
};
exports.update = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  res.status(200).send(API._200(await db_helper.update('LiveEvents', input)));
};
exports.add_recording = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  let data = {
    id: input.event_id,
    is_completed: 1,
    complete_date: new Date().toISOString()
  }
  await db_helper.update('LiveEvents', data);
  res.status(200).send(API._200(await db_helper.addData('EventRecording', input)));
};
exports.all_live_event_add = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  let all_live_events = await db_helper.query("delete from all_live_events where live_event_id=" + input.live_event_id);
  var live_event_data = input;
  const array_start_date = JSON.parse(live_event_data.array_start_date)
  var arraylive_event = array_start_date.map(function (el) {
    var o = Object.assign({}, el);
    for (const [key, value] of Object.entries(live_event_data)) {
      if (key == "start_date") {
        o[key] = o.date;
      } else {
        o[key] = value;
      }
    }
    return o
  })

  res.status(200).send(API._200(await db['AllLiveEvent'].bulkCreate(arraylive_event)));
};
exports.all_live_event_Start_time_event = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  let all_live_events = await db_helper.query("select * from all_live_events where live_event_id=" + input.id);
  if (all_live_events[0].length > 0) {
    res.status(200).send(API._200(await all_live_events[0]));
  } else {
    let live_events = await db_helper.query("select * from live_events where id=" + input.id);
    res.status(200).send(API._200(await live_events[0]));
  }
};
exports.all_live_event_update = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  let all_live_events = await db_helper.query("select * from all_live_events where live_event_id=" + input.live_event_id + " and DATE(start_date)='" + input.startdate + "'");
  if (all_live_events[0].length > 0) {
    input["id"] = all_live_events[0][0].id;
    let data = input;
    res.status(200).send(API._200(await db_helper.update("AllLiveEvent", data)));
  } else {
    let live_events = await db_helper.query("select * from live_events where id=" + input.live_event_id);
    if (live_events[0].length > 0) {
      input["id"] = input.live_event_id;
      let data = input;
      res.status(200).send(API._200(await db_helper.update("LiveEvents", data)));
    }
  }
};
exports.get_all_event_recordings = async (req, res) => {
  const data = await db.sequelize.query("select * from event_recordings where play_list!='[]' and status=0")
    .then(function (data) {
      return Array.from(new Set(data[0]));
    })
  res.status(200).send(API._200(await data));
};
exports.get_attendy = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  var data = await db_helper.query("select userslist,id from cohorts where find_in_set(" + input.id + ",live_events) and status=0 and userslist<>''");
  res.status(200).send(API._200(await data));
};
exports.get_attendy_selfpaced = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  let table_name = "self_paced_one_times"
  if (input.course_type == 'subscription') {
    table_name = "self_paced_subscriptions"
  } else if (input.course_type == 'free') {
    table_name = "self_paceds";
  } else {
    table_name = "self_paced_one_times"
  }
  var data = await db_helper.query("select userslist,id from cohorts where id in(select cohort_id  from " + table_name + " where find_in_set(" + input.id + ",live_events) and status=0)");
  res.status(200).send(API._200(await data));
};
exports.update_event_recordings_url = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  let upd = "";
  if (input.questions != null) {
    upd = "questions='" + input.questions + "'";
  } else {
    upd = "questions=null"
  }
  const data = await db.sequelize.query("update event_recordings set " + upd + ", recording_url='" + input.recording_url + "', recording_url_144p='" + input.recording_url_144p + "', recording_url_240p='" + input.recording_url_240p + "', recording_url_360p='" + input.recording_url_360p + "', recording_url_480p='" + input.recording_url_480p + "', recording_url_720p='" + input.recording_url_720p + "' where id=" + input.id)
  res.status(200).send(API._200(await data));
};
exports.event_byids = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  var data = await db_helper.query("select * from live_events where id in(" + input.ids + ")");
  res.status(200).send(API._200(await data));
};
