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

exports.flashcardGetById = async (req, res) => {
  const flashcardId = await helper.decryptUri(req.params.id)
  const flashcard = await Flashcard.findOne({
    where: {
      id: flashcardId,
      status: 0,
    }
  });
  if (!flashcard) {
    res.status(404).send(API._404({ message: `Flashcard with id: ${id} was not found` }));
  }
  res.status(200).send(API._200(await flashcard));
};
exports.deleteFlashcard = async (req, res) => {
  const flashcardId = await helper.decryptUri(req.params.id)
  const flashcard = await Flashcard.findByPk(flashcardId).then(async res => {
    res.set("status", 1);
    return await res.save();
  })
  res.status(200).send(API._200(await flashcard));
};
exports.updateFlashcard = async (req, res) => {
  const id = await helper.decryptUri(req.params.id)
  const flashcard = await Flashcard.findByPk(id)
  if (!flashcard) {
    res.status(404).send(API._404({ message: `Flashcard with id: ${id} was not found` }));
  }
  if (input.free_paid) flashcard.free_paid = input.free_paid
  if (input.d_id) flashcard.d_id = input.d_id
  if (input.sd_id) flashcard.sd_id = input.sd_id
  if (input.topic_id) flashcard.topic_id = input.topic_id
  if (input.certi_id) flashcard.certi_id = input.certi_id
  if (input.term) flashcard.term = input.term
  if (input.definition) flashcard.definition = input.definition
  if (input.ref_link) flashcard.ref_link = input.ref_link
  if (input.youtube_link) flashcard.youtube_link = input.youtube_link
  if (input.bookpage_no) flashcard.bookpage_no = input.bookpage_no
  if (input.blog_url) flashcard.blog_url = input.blog_url
  if (input.point) flashcard.point = input.point
  if (input.status) flashcard.status = input.status
  res.status(200).send(API._200(await flashcard.save()));
};
exports.getAllFlashcard = async (req, res) => {
  const flashcard = Flashcard.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 flashcard));
};
exports.addFlashcard = async (req, res) => {
  const data = await helper.decryptRequest(req.body.data);
  const flashcard = await Flashcard.create(data);
  res.status(200).send(API._200(await flashcard));
};
exports.aggregate = async (req, res) => {
  const input = await helper.decryptRequest(req.body.data);
  var AddQ = '';
  if (input.certi_id && input.certi_id != '') {
    AddQ = "domains.certi_id=" + input.certi_id + " AND";
  }
  var sql = "SELECT *," +
    "(SELECT count(*) FROM flashcards WHERE flashcards.d_id=domains.id AND status=0) as ttlcards," +
    "(SELECT count(*) FROM results WHERE results.domain_id=domains.id AND ans=1 AND active=1 AND user_id=" + input.u_id + ") as ttlright," +
    "(SELECT count(*) FROM results WHERE results.domain_id=domains.id AND ans=2 AND active=1  AND user_id=" + input.u_id + ") as total_review," +
    "(SELECT count(*) FROM results WHERE results.domain_id=domains.id AND ans=3 AND active=1  AND user_id=" + input.u_id + ") as total_learn_more" +
    " FROM  domains where " + AddQ + " domains.status=0";

  const result = await db.sequelize.query(sql)
    .then(function (data) {
      return Array.from(new Set(data[0]));
    })
  var totalcards = 0;
  var totolright = 0;
  var totalreviews = 0;
  var totallearnmore = 0;
  if (result.length > 0) {
    for (let i = 0; i < result.length; i++) {
      totalcards += result[i].ttlcards;
      totolright += result[i].ttlright;
      totalreviews += result[i].total_learn_more;
      totallearnmore += result[i].total_review;
    }
  }
  res.status(200).send(API._200(await [{ 'totalcards': totalcards, 'totolright': totolright, 'totalreviews': totalreviews, 'totallearnmore': totallearnmore }]));
};