const Quartions = require("../model/auto/Quartions");
const Answare = require("../model/auto/Answare");
const Sport = require("../model/Sport");
const Controller = require("./Controller");
const Match = require("../model/Match");
module.exports = new class QuartionController extends Controller {
    async add(req, res, next) {
        try {

            const {
                quartion,
                sport,
                resuld_type,
                ans_add,
                runs,
                rets,
                overs,
                answares = []
            } = req.body;

            const payload = {
                title: quartion,
                sport_id: sport,
                resuld_type,
                is_adds: ans_add ? "yes" : "no",
                is_runs: runs ? "yes" : "no",
                is_rets: rets ? "yes" : "no",
                is_overs: overs ? "yes" : "no"
            };
            console.time("start");
            const q_id = await Quartions.add(payload);
            if (answares.length) {
                await Promise.all(
                    answares.map(item =>
                        Answare.add({
                            title: item.title,
                            quarion_id: q_id.id,
                            ret: item.ret
                        })
                    )
                );
            }
            console.timeEnd("start");
            return res.success("ok");
        } catch (error) {
            return next(error);
        }
    }
    async index(req, res, next) {
        try {
            const autodata = await Quartions.get().orderBy("id", "desc");
            const resuld = [];
            if (autodata.length > 0) {
                for (const item of autodata) {
                    const data = await Answare.get().where("quarion_id", "=", item.id);
                    resuld.push({
                        ...item,
                        is_adds: item.is_adds === "yes",
                        is_runs: item.is_runs === "yes",
                        is_rets: item.is_rets === "yes",
                        is_overs: item.is_overs === "yes",
                        ans: data
                    })
                }
                return res.success("ok", resuld)
            } else {
                return res.error("data not found");
            }
        } catch (error) {
            console.log(error.toString());
            return next(error);
        }
    }
    async delete(req, res, next) {
        try {
            const id = req.params.id;
            await Quartions.del(id);
            await Answare.where("quarion_id", "=", id).del()
            return res.success("ok")
        } catch (error) {
            return next(error);
        }
    }
    async update(req, res, next) {
        try {
            const id = req.params.id;
            const {
                quartion,
                sport,
                resuld_type,
                ans_add,
                runs,
                rets,
                overs,
                answares = []
            } = req.body;
            const payload = {
                title: quartion,
                sport_id: sport,
                resuld_type,
                is_adds: ans_add ? "yes" : "no",
                is_runs: runs ? "yes" : "no",
                is_rets: rets ? "yes" : "no",
                is_overs: overs ? "yes" : "no"
            };
            const q_id = await Quartions.update(id, (old) => (payload));
            if (answares.length) {
                const existingAnswers = await Answare.get("id").where("quarion_id", "=", id);
                const isIds = existingAnswers.map((item) => item.id);
                await Promise.all(
                    answares.map(async item => {
                        if (isIds.includes(item.id)) {
                            // update
                            return Answare.update(item.id, () => (
                                {
                                    title: item.title,
                                    ret: item.ret
                                }
                            ))
                        } else {
                            // add
                            return Answare.add({
                                title: item.title,
                                quarion_id: q_id.id,
                                ret: item.ret
                            })
                        }
                    })
                );
            }
            return res.success("update successfull");
        } catch (error) {
            return next(error);
        }
    }

    // ans -------------------
    async ansDlete(req, res, next) {
        try {
            const id = req.params.id;
            await Answare.del(id);
            return res.success("ok")
        } catch (error) {
            return next(error);
        }
    }
    // ------------------------------
    getforadd = async (req, res, next) => {
        try {
            const match_id = req.params.sport;
            const match_data = await Match.find(match_id);
            if (!match_data) return next("Data not found");
            const datas = await Quartions.get().where("sport_id", "=", match_data.sport_id).orWhere("sport_id", "=", 'global');
            const responce = datas.map(item => ({
                title: item.title,
                id: item.id,
            }))
            return res.success("success", responce);
        } catch (error) {
            return next(error);
        }
    }
}