// const { filelocation, removefile, uploadfile, genaretname, getfile } = require("../middleware/uploadMiddleware");
const Admin = require("../model/Admin");
const Method = require("../model/Method");
const Controller = require("./Controller");
module.exports = new class MethodsController extends Controller {

    index = async (req, res, next) => {
        try {
            const data = await Method.where("is_active", Method.is_active.yes);

            const newData = await Promise.all(
                data.map(async (item) => {

                    let creator_name = null;
                    let editor_name = null;

                    if (item.creator_id) {
                        const creator = await Admin.find(item.creator_id);
                        creator_name = creator?.name || null;
                    }

                    if (item.editor_id) {
                        const editor = await Admin.find(item.editor_id);
                        editor_name = editor?.name || null;
                    }

                    return {
                        ...item,
                        creator_id: creator_name,
                        editor_id: editor_name
                    };
                })
            );

            return res.success('method list success', newData);

        } catch (error) {
            return next(error);
        }
    };
    delete = async (req, res, next) => {
        try {
            const id = req.params['id'];
            await Method.del(id);
            return res.success('method deleted success')
        } catch (error) {
            return next(error);
        }
    }
    update = async (req, res, next) => {
        try {
            const id = req.params['id'];
            await Method.find(id);
            const payload = req.body;
            payload.editor_id = req.user.id;
            payload.is_active = "yes";
            await Method.update(id, () => payload);
            return res.success('method updated success')
        } catch (error) {
            return next(error);
        }
    }
    store = async (req, res, next) => {
        try {
            const { name, number, type } = req.body;
            if (!name || !number || !type) return next("input field empty");
            const payload = req.body;
            payload.creator_id = req.user.id;
            payload.logo = "";
            payload.is_active = "yes";
            await Method.add(payload);
            return res.success('method created success')
        } catch (error) {
            return next(error);
        }
    }
    get = async (req, res, next) => {
        try {
            const data = await Method.where("is_active", "yes");
            return res.success("method data", data);
        } catch (error) {
            return next(error.toString());
        }

    }
    active = async (req, res, next) => {
        try {
            const id = req.params['id'];
            await Method.find(id);
            await Method.update(id, (v) => ({ is_active: v.is_active == Method.is_active.no ? Method.is_active.yes : Method.is_active.no }));
            return res.success('method updated success')
        } catch (error) {
            return next(error);
        }
    }
}