const db = require("../models");
const API = require('../helper/API_Response');
const db_helper = require("../helper/db_helper");
const helper = require("../helper/helper");
const { Acronyms, Certificate, StudyMaterial, Domain, Subdomain, Topic, Cource, Video } = require("../models");

exports.studyMaterialGetById = async (req, res) => {
  const studyMaterialId = await helper.decryptUri(req.params.id)
  const studyMaterial = await StudyMaterial.findOne({
    where: {
      id: studyMaterialId,
      status: 0,
    },
    include: [{
      model: Domain,
      attributes: ['id', 'domin_name']
    }, {
      model: Subdomain,
      attributes: ['id', 'subdomain_name']
    }, {
      model: Topic,
      attributes: ['id', 'topic_name']
    }, {
      model: Certificate,
      attributes: ['id', 'certificate_name']
    }]
  });
  if (!studyMaterial) {
    res.status(404).send(API._404({ message: `studyMaterial with id: ${id} was not found` }));
  }
  res.status(200).send(API._200(await studyMaterial));
};
exports.deleteStudyMaterial = async (req, res) => {
  const studyMaterialId = await helper.decryptUri(req.params.id)
  const studyMaterial = await StudyMaterial.findByPk(studyMaterialId).then(async res => {
    res.set("status", 1);
    return await res.save();
  })
  res.status(200).send(API._200(await studyMaterial));
};
exports.updateStudyMaterial = async (req, res) => {
  const id = await helper.decryptUri(req.params.id)
  const input = await helper.decryptRequest(req.body.data);
  const studyMaterial = await StudyMaterial.findByPk(id)
  if (!studyMaterial) {
    res.status(404).send(API._404({ message: `Certificate with id: ${id} was not found` }));
  }
  if (input.d_id) studyMaterial.d_id = input.d_id
  if (input.sd_id) studyMaterial.sd_id = input.sd_id
  if (input.certi_id) studyMaterial.certi_id = input.certi_id
  if (input.cource_id) studyMaterial.cource_id = input.cource_id
  if (input.topic_id) studyMaterial.topic_id = input.topic_id
  if (input.subtopic_id) studyMaterial.subtopic_id = input.subtopic_id
  if (input.title) studyMaterial.title = input.title
  if (input.content) studyMaterial.content = input.content
  if (input.status) studyMaterial.status = input.status
  if (input.pdf_files) studyMaterial.pdf_files = input.pdf_files
  if (input.ppt_files) studyMaterial.ppt_files = input.ppt_files
  if (input.image_files) studyMaterial.image_files = input.image_files
  if (input.excel_files) studyMaterial.excel_files = input.excel_files

  res.status(200).send(API._200(await studyMaterial.save()));
};
exports.getAllStudyMaterial = async (req, res) => {
  const studyMaterial = StudyMaterial.findAll({
    where: {
      status: 0
    },
    include: [{
      model: Domain,
      attributes: ['id', 'domin_name']
    }, {
      model: Subdomain,
      attributes: ['id', 'subdomain_name']
    }, {
      model: Topic,
      attributes: ['id', 'topic_name']
    }, {
      model: Certificate,
      attributes: ['id', 'certificate_name']
    }, {
      model: Cource,
      attributes: ['id', 'course_name']
    }]
  });
  res.status(200).send(API._200(await studyMaterial));
};
exports.addStudyMaterial = async (req, res) => {
  const data = await helper.decryptRequest(req.body.data);
  const studyMaterial = await StudyMaterial.create(data);
  res.status(200).send(API._200(await studyMaterial));
};
exports.getMaterialAssigned = async (req, res) => {
  const data = await helper.decryptRequest(req.body.data);
  const domains = await Domain.findAll({
    attributes: ['id', 'domin_name', 'domain_number'],
    where: {
      certi_id: data.cource_id,
      status: 0
    }
  });
  let dArray = []
  for (let d = 0; d < domains.length; d++) {
    const SubDomains = await Subdomain.findAll({
      attributes: ['id', 'subdomain_name', 'domain_number', 'no_assign_note'],
      where: {
        d_id: domains[d].id,
        status: 0
      }
    });
    let sdArray = []
    for (let s = 0; s < SubDomains.length; s++) {
      const topics = await Topic.findAll({
        where: {
          sd_id: SubDomains[s].id,
          status: 0
        }
      });
      let tArray = []
      for (let t = 0; t < topics.length; t++) {
        const studyMaterial = await StudyMaterial.findAll({
          attributes: ['id',
            'title',
            'content',
            'pdf_files',
            'ppt_files',
            'image_files',
            'excel_files',
          ],
          where: {
            topic_id: topics[t].id,
            status: 0
          },
        })

        const videos = await Video.findAll({
          attributes: ['id', 'video_title', 'video_thumbnil', 'duration', 'type', 'topic_list', 'video_link'],
          where: {
            topic_id: topics[t].id,
            status: 0
          },
        })
        // var countData = await db_helper.selectByWhere('VideoMindView', { cohort_id: input.cohort_id, user_id: input.user_id, is_completed: 1 });
        // if (countData.length > 0) {
        //   let pdfCount = countData.filter(x => x.type == 'PDF' && x.type_id == studyMaterial[0].id)
        //   let pptCount = countData.filter(x => x.type == 'PPT' && x.type_id == studyMaterial[0].id)
        //   let excelCount = countData.filter(x => x.type == 'EXCEL' && x.type_id == studyMaterial[0].id)
        //   let imageCount = countData.filter(x => x.type == 'IMAGE' && x.type_id == studyMaterial[0].id)
        // }
        tArray.push({
          id: topics[t].id,
          topic_name: topics[t].topic_name,
          is_check: false,
          percent: 0,
          wpercent: 0,
          Summary: studyMaterial,
          Video: videos,
        })
      }
      sdArray.push({
        id: SubDomains[s].id,
        subdomain_name: SubDomains[s].subdomain_name,
        domain_number: SubDomains[s].domain_number,
        no_assign_note: SubDomains[s].no_assign_note,
        is_check: false,
        percent: 0,
        wpercent: 0,
        topics: tArray,
      })
    }
    dArray.push({
      id: domains[d].id,
      domin_name: domains[d].domin_name,
      domain_number: domains[d].domain_number,
      sublen: sdArray.length,
      is_check: false,
      percent: 0,
      wpercent: 0,
      subdomains: sdArray
    })

  }

  res.status(200).send(API._200(await dArray));
};