const bcrypt = require("bcrypt");
const Admin = require("../model/Admin");
const Match = require("../model/Match");
const Betting = require("../model/Betting");
module.exports = new class AdminController extends require("./Controller") {

    add = async (req, res, next) => {
        try {
            const { name, email, password } = req.body;
            if (!name || !email || !password) return next("Input field empty");
            if (password.length < 6) return next("Password minimum 6 character");
            const exists = await Admin.where("email", email);
            if (exists.length > 0) return next("This email already exist");
            const hashed = await bcrypt.hash(password, 10);
            await Admin.add({ name, email, password: hashed });
            return res.success("User add successfull");
        } catch (error) {
            return next(error.toString());
        }
    };
    gammer = async (req, res, next) => {
        try {
            const users = await Admin.where("role", "=", "gamer").where("trash", "=", Admin.trash.no);
            const gamerList = [];
            for (let user of users) {
                const total_match = await Match.where("creator_id", user.id);
                const totalBet = await Betting.where("resulder_id", user.id);
                const created_match = total_match.length;
                const active_match = total_match.filter(m => m.type == "live").length;
                const pending_match = total_match.filter(m => m.type == "comming").length;
                const total_loss = totalBet.filter(it => it.status == "win").reduce((o, d) => o += parseFloat(d.return_taka),0);
                const total_win = totalBet.filter(it => it.status == "loss").reduce((o, d) => o += parseFloat(d.taka),0);
                const finly_profite = (total_loss - total_win).toFixed(0).replace("-","loss ") ;
                gamerList.push({
                    id: user.id,
                    name: user.name,
                    email: user.email,
                    status: user.is_active,
                    total_win,
                    total_loss,
                    finly_profite,
                    created_match,
                    active_match,
                    pending_match,
                })
            }
            return res.success("All gamer", gamerList);
        } catch (error) {
            return next(error.toString());
        }
    };
    passupdate = async (req, res, next) => {
        try {
            const {  npass } = req.body;
            if (!npass) return next("Input field empty");
            // if (old === npass) return next("Old and new password same!");
            if (npass.length < 6) return next("Password minimum 6 character");
            const user = req.user;
            // const valid = await bcrypt.compare(old, user.password);
            // if (!valid) return next("Invalid Old password");
            const hashed = await bcrypt.hash(npass, 10);
            await Admin.update(user.id, v => ({ password: hashed }));
            return res.success("Password update successfull");
        } catch (error) {
            return next(error.toString());
        }
    };
    remove = async (req, res, next) => {
        try {
            const { id } = req.params;
            await Admin.find(id);
            await Admin.update(id, v => ({ trash: Admin.trash.yes }));
            return res.success("successfull");
        } catch (error) {
            return next(error.toString());
        }
    };
    block = async (req, res, next) => {
        try {
            const { id } = req.params;
            const data = await Admin.find(id);
            await Admin.update(id, v=>({is_active:v.is_active=="0"?"1":"0"}));
            return res.success("successfull");
        } catch (error) {
            return next(error);
        }
    };
    profile = async (req, res, next) => {
        try {
            const user = req.user;
            const total_loss = 0;
            const total_win = 0;
            const finly_profite = total_win - total_loss;
            const created_match = 50;
            const active_match = 5;
            const pending_match = 100;
            // responce data on client
            return res.success("your profile", {
                name: user.name,
                email: user.email,
                role: user.role,
                status: user.status,
                total_win,
                total_loss,
                finly_profite,
                created_match,
                active_match,
                pending_match,
            });
        } catch (error) {
            return next(error.toString());
        }
    };
};