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

exports.getCohortForId = async (req, res) => {
  const cohortId = await helper.decryptUri(req.params.id)
  let output = new Promise(async (resolve, reject) => {
    const cohort = Cohort.findAll({
      where: {
        id: cohortId,
        status: 0
      },
      include: [{
        model: Certificate,
        attributes: ['id', 'certificate_name']
      }]
    }).then(async (res) => {
      if (res.length == 0) { reject() };
      for (let j = 0; j < res.length; j++) {
        res[j] = res[j].toJSON()
        const knowassessments = await res[j].knowassessments;
        const practicetests = await res[j].practicetests;
        const userslist = await (res[j].userslist ? res[j].userslist : []);
        let data1 = await KnowledgeAsses.findAll({
          attributes: ['id', 'knowass_name'],
          where: {
            status: 0,
            id: knowassessments
          }
        });
        res[j].knowledge_asses = data1.map((a) => a.toJSON())
        let data2 = await Practicetest.findAll({
          attributes: ['id', 'practice_name'],
          where: {
            status: 0,
            id: practicetests
          }
        })
        res[j].practicetests = data2.map((a) => a.toJSON())

        let data3 = await User.findAll({
          attributes: ['id', 'name'],
          where: {
            status: 0,
            id: userslist
          }
        })

        res[j].user = data3.map((a) => a.toJSON())
        if ((j + 1) == res.length) {
          resolve(res);
        }
      }
    });
  }).catch((ex) => {
    res.status(404).send(API._404({ message: `Cohort was not found` }));
  })
  res.status(200).send(API._200(await output));
};
exports.updateCohort = async (req, res) => {
  const cohortId = await helper.decryptUri(req.params.id)
  const cohort = await Cohort.findByPk(cohortId)
  if (!cohort) {
    res.status(404).send(API._404({ message: `Cohort with id: ${cohortId} was not found` }));
  }
  if (input.cohort_name) cohort.cohort_name = input.cohort_name
  if (input.certi_id) cohort.certi_id = input.certi_id
  if (input.location) cohort.location = input.location
  if (input.userslist) {
    cohort.userslist = input.userslist
  }
  if (input.knowassessments) cohort.knowassessments = input.knowassessments
  if (input.practicetests) {
    cohort.practicetests = input.practicetests;
  }
  if (input.cource_id) cohort.cource_id = input.cource_id
  if (input.status) cohort.status = input.status
  if (input.practice_time) cohort.practice_time = input.practice_time;

  res.status(200).send(API._200(await cohort.save()));
};
exports.deleteCohort = async (req, res) => {
  const cohortId = await helper.decryptUri(req.params.id)
  const cohort = await Cohort.findByPk(cohortId);
  if (!cohort) {
    res.status(404).send(API._404({ message: `Cohort with id: ${cohortId} was not found` }));
  }
  cohort.status = 1
  await db_helper.query("update users set course_id = null  where course_id =" + cohortId);
  await db_helper.query("delete from cohort_schedules where cohort_id=" + cohortId);
  res.status(200).send(API._200(await cohort.save()));
};
exports.getAllCohorts = async (req, res) => {
  var result = new Promise(async (resolve, reject) => {
    const cohort = Cohort.findAll({
      where: {
        status: 0,
        id: 1
      },
      include: [{
        model: Certificate,
        attributes: ['id', 'certificate_name']
      }, {
        model: Cource,
        attributes: ['id', 'course_name']
      }]
    }).then(async (res1) => {
      if (res1.length == 0) { reject() };
      for (let j = 0; j < res1.length; j++) {
        res1[j] = res1[j]
        const knowassessments = await res1[j].knowassessments;
        const practicetests = await res1[j].practicetests;
        const CATExam = await res1[j].ketexamtests;
        const userslist = await (res1[j].userslist ? res1[j].userslist : []);

        let data1 = await KnowledgeAsses.findAll({
          attributes: ['id', 'knowass_name'],
          where: {
            status: 0,
            id: knowassessments
          }
        });

        res1[j].knowassessments = data1.map((a) => a.toJSON())

        let data2 = await Practicetest.findAll({
          attributes: ['id', 'practice_name'],
          where: {
            status: 0,
            id: practicetests
          }
        })
        res1[j].practicetests = data2.map((a) => a.toJSON())


        let data3 = await User.findAll({
          attributes: ['id', 'name'],
          where: {
            status: 0,
            id: userslist
          }
        })
        res1[j].userslist = data3.map((a) => a.toJSON())

        let data4 = await Ketexamtest.findAll({
          attributes: ['id', 'ketexam_name'],
          where: {
            status: 0,
            id: CATExam
          }
        })
        res1[j].ketexamtests = data4.map((a) => a.toJSON())

        if ((j + 1) == res1.length) {
          resolve(res1);
        }
      }
    });
  }).catch((ex) => {
    res.status(404).send(API._404({ message: 'Cohort was not found' }));
  })
  res.status(200).send(API._200(await result));
};