const db = require("../models");
const API = require('../helper/API_Response');
const db_helper = require("../helper/db_helper");
const helper = require("../helper/helper");
const { Domain, Subdomain, Video, MindMap, Topic, References, Version } = 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("Variables", input)));
};
exports.all = async (req, res) => {
  res.status(200).send(API._200(await db_helper.allData("Variables")));
};
exports.certificate_materials = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  const domain = await Domain.findAll({
    attributes: ['id', 'domin_name'],
    where: {
      status: 0,
      certi_id: input.certi_id
    }
  });
  let dom = [];
  for (let i = 0; i < domain.length; i++) {
    const subdomain = await Subdomain.findAll({
      attributes: ['id', 'subdomain_name'],
      where: {
        status: 0,
        d_id: domain[i].id,
        certi_id: input.certi_id
      }
    });
    let sd = [];
    for (let j = 0; j < subdomain.length; j++) {
      const topic = await Topic.findAll({
        attributes: ['id', 'topic_name'],
        where: {
          status: 0,
          sd_id: subdomain[j].id,
          certi_id: input.certi_id
        }
      });
      let tpk = [];
      for (let t = 0; t < topic.length; t++) {
        const refer = await References.findAll({
          attributes: ['id', 'title', 'page_no'],
          where: {
            status: 0,
            topic_id: topic[t].id,
            certi_id: input.certi_id
          }
        });
        var study = await db_helper.query("select id,title from study_materials where status=0 and topic_id=" + topic[t].id +
          "; select id,question from know_ass_ques where status=0 and certi_id=" + input.certi_id + " and topic_id=" + topic[t].id +
          "; select term,definition from flashcards where status=0 and topic_id=" + topic[t].id + " and certi_id=" + input.certi_id +
          "; select id,title from practiceques where status=0 and topic_id=" + topic[t].id + " and certi_id=" + input.certi_id);
        tpk.push({
          id: topic[t].id,
          name: topic[t].topic_name,
          Reference: refer,
          WrittenSummary: study[0][0].length > 0 ? "Yes" : "None",
          KAQ: study[0][1],
          FlashCards: study[0][2],
          PTQ: study[0][3]
        })
      }
      const video = await db.sequelize.query("select id,video_title,duration from videos where status=0 and certi_id=" + input.certi_id + " and find_in_set(" + subdomain[j].id + ",sd_id) ")
        .then(function (data) {
          return Array.from(new Set(data[0]));
        });
      const mind = await db.sequelize.query("select id,name,duration from mind_maps where status=0 and certi_id=" + input.certi_id + " and find_in_set(" + subdomain[j].id + ",sd_id) ")
        .then(function (data) {
          return Array.from(new Set(data[0]));
        });

      sd.push({
        id: subdomain[j].id,
        subdomain_name: subdomain[j].subdomain_name,
        Videos: video,
        MindMaps: mind,
        Topics: tpk
      })
    }
    dom.push({
      id: domain[i].id,
      domin_name: domain[i].domin_name,
      SubDomains: sd
    })
  }
  res.status(200).send(API._200(await dom));
};
exports.delete = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  res.status(200).send(API._200(await db_helper.statusChange("Variables", input.id)));
};
exports.get_all_app_version = async (req, res) => {
  const all = await Version.findAll({
    where: {
      status: 0,
      certy_id: 11
    }
  });
  res.status(200).send(API._200(await all));
};
exports.update = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  res.status(200).send(API._200(await db_helper.update("Variables", input)));
};
exports.update_app_version = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  let all = await db_helper.query("UPDATE versions set version_android='" + input.version_android + "',version_ios='" + input.version_ios + "',version_android_ccsp='" + input.version_android_ccsp + "',version_ios_ccsp='" + input.version_ios_ccsp + "',version_android_practice='" + input.version_android_practice + "',version_ios_practice='" + input.version_ios_practice + "'");
  res.status(200).send(API._200(await all[0]));
};