const Admin = require("../model/Admin");
const Sport = require("../model/Sport");
const Controller = require("./Controller")

class SpoertController extends Controller {
    add = async (req, res, next) => {
        try {
            const { logo, name, word } = req.body;
            if (!logo || !name || !word) return next("Input feild empty");
            const user = req.user;
            await Sport.add({
                "name": name,
                "icon": logo,
                "key_word": word,
                "creator_name": user.id
            });
            return res.success("Sport add successfull")
        } catch (error) {
            return next(error.toString());
        }
    }
    all = async (req, res, next) => {
        try {
            const datas = await Sport.get().orderBy("id", "desc");
            if (!datas || datas.length === 0) {
                return next("Sport not found");
            }
            //                 value.bet,
            const newdata = [];
            for (const data of datas) {
                const creator = await Admin.where("id", data.creator_name).first();
                newdata.push({
                    "logo": data.icon,
                    "word": data.key_word.split(","),
                    "name": data.name,
                    "creator": creator?.name,
                    "live": 1,
                    "upcoming": 1,
                    "complate": 1,
                    "id": data.id,
                    "block": data.status === "deactive"
                })
            }
            return res.success("Sport add successfull", newdata);
        } catch (error) {
            return next(error.toString());
        }
    }
    update = async (req, res, next) => {
        try {
            const { id } = req.params;
            const { logo, name, word } = req.body;
            if (!logo || !name || !word || !id) return next("Input feild empty");
            const user = req.user;
            await Sport.update(id, (value) => ({
                "name": name,
                "icon": logo,
                "key_word": word,
                "updator_name": user.id
            }));
            return res.success("update success");
        } catch (error) {
            return next(error.toString());
        }
    }
    remove = async (req, res, next) => {
        try {
            const { id } = req.params;
            if (!id) return next("Input feild empty");
            await Sport.del(id);
            return res.success("remove success");
        } catch (error) {
            return next(error.toString());
        }
    }
    block = async (req, res, next) => {
        try {
            const { id } = req.params;
            if (!id) return next("Input feild empty");
            await Sport.update(id, (value) => {
                const status = value.status == "active" ? "deactive" : "active";
                return { status }
            });
            return res.success("success");
        } catch (error) {
            return next(error.toString());
        }
    }
    keyworid = async (req, res, next) => {
        try {
            const { id } = req.params;
            const dataArr = await Sport.get("key_word").where("id", "=", id);
            if (!dataArr || !dataArr[0]) {
                return next("Sport not found");
            }
            const key_word = dataArr[0].key_word || "";
            const keys = key_word.split(",").map(k => k.trim()).filter(k => k.length > 0);
            return res.success("key data", keys);
        } catch (error) {
            return next(error.toString());
        }
    }
}
module.exports = new SpoertController();