const db = require("../models");
const API = require('../helper/API_Response');
const db_helper = require("../helper/db_helper");
const helper = require("../helper/helper");
const { User, Version, Navigation, Onbording } = require("../models");

exports.get_dashboard_domain = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  const response = await db.sequelize.query("select * from self_paced_materials where method_id= :method_id and method_type='" + input.method_type + "' and material_type='title' and title_type='Section' and title_name like '%Domain%'", { replacements: { method_id: input.method_id } })
    .then(function (data) {
      return Array.from(new Set(data[0]));
    })
  res.status(200).send(API._200(await response));
};
exports.add = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  res.status(200).send(API._200(await db_helper.addData('VideoDiscussion', input)));
};

exports.by = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  input.status = 0;
  const a = await VideoDiscussion.findAll({
    where: input,
    include: [{
      model: User,
      attributes: ['id', 'name', 'email', 'avatar_url']
    }]
  });
  res.status(200).send(API._200(await a));
};

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

exports.review_guide = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  const all = await db.sequelize.query("select knowassessments, practicetests,ttlcards,case when ttlview>ttlcards then ttlcards else ttlview end ttlview from cohorts left join (select count(distinct cid) ttlview,IFNULL((SELECT COUNT(*) as ttlcards FROM flashcards WHERE certi_id in(select certi_id from cohorts where id= :cohort_id) and status=0),0) AS ttlcards from flashans_offlines where UserId= :user_id and cohort_id= :cohort_id) aa on 1=1 where id = :cohort_id", { replacements: { cohort_id: input.cohort_id, user_id: input.user_id } })

    .then(function (data) {
      return Array.from(new Set(data[0]));
    })
  let result = { total_ka: 0, total_pt: 0, complete_ka: 0, complete_pt: 0, ttlcards: 0, ttlview: 0 }

  if (all.length > 0) {
    result = {
      total_ka: all[0].knowassessments ? all[0].knowassessments.split(",").length : 0,
      total_pt: all[0].practicetests ? all[0].practicetests.split(",").length : 0,
      ttlcards: all[0].ttlcards,
      ttlview: all[0].ttlview
    }
    if (all[0].knowassessments) {
      const ka_complete = await db.sequelize.query("select count(*) as ttl from knowcompletes where user_id= :user_id and cohort_id= :cohort_id and ka_id in(" + all[0].knowassessments + ")", { replacements: { cohort_id: input.cohort_id, user_id: input.user_id } })
        .then(function (data) {
          return Array.from(new Set(data[0]));
        })
      if (ka_complete.length > 0) {
        result.complete_ka = ka_complete[0].ttl
      }
    }
    if (all[0].practicetests) {
      const pt_complete = await db.sequelize.query("select count(*) as ttl from ptestcomplates where user_id= :user_id and cohort_id= :cohort_id and pt_id in(" + all[0].practicetests + ")", { replacements: { cohort_id: input.cohort_id, user_id: input.user_id } })
        .then(function (data) {
          return Array.from(new Set(data[0]));
        })
      if (pt_complete.length > 0) {
        result.complete_pt = pt_complete[0].ttl
      }
    }
  }

  res.status(200).send(API._200(await result));
};
exports.get_event_recordings = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  let query = "select live_events from cohorts where id= :cohort_id";
  if (input.method_type == 'subscription') {
    query = "select live_events from self_paced_subscriptions where cohort_id= :cohort_id";
  } else if (input.method_type == 'free') {
    query = "select live_events from self_paceds where cohort_id= :cohort_id";
  } else if (input.method_type == 'onetime') {
    query = "select live_events from self_paced_one_times where cohort_id= :cohort_id";
  } else if (input.method_type == 'live') {
    query = "select live_events from cohorts where id= :cohort_id";
  }
  const cohort_data = await db.sequelize.query(query, { replacements: { cohort_id: input.cohort_id } })
    .then(function (data) {
      return Array.from(new Set(data));
    })
  if (cohort_data[0][0].live_events) {
    const data = await db.sequelize.query("select * from event_recordings where status=0 and event_id in(" + cohort_data[0][0].live_events + ")")
      .then(function (data) {
        return Array.from(new Set(data[0]));
      })
    res.status(200).send(API._200(await data));
  } else {
    res.status(200).send(API._200([]));
  }
};