// Task Team — Odoo Module Management — 2026-03-30
async function odooModuleRoutes(app) {
    const ODOO_ENT = "http://10.10.10.20:8069";
    const ODOO_COM = "http://10.10.10.20:8070";

    app.get("/odoo/modules", async (req) => {
        return { data: {
            available: ["task_team_connector"],
            location: "/opt/task-team/odoo_modules/",
            enterprise_url: ODOO_ENT,
            community_url: ODOO_COM
        }};
    });

    app.post("/odoo/modules/install", async (req) => {
        const { module_name, server } = req.body;
        const url = server === "community" ? ODOO_COM : ODOO_ENT;
        // Trigger module install via Odoo JSON-RPC
        try {
            const authRes = await fetch(url + "/jsonrpc", {
                method: "POST", headers: {"Content-Type":"application/json"},
                body: JSON.stringify({jsonrpc:"2.0",method:"call",id:1,
                    params:{service:"common",method:"authenticate",
                    args:[server==="community"?"odoo_community":"odoo_enterprise","admin","admin",{}]}})
            });
            const uid = (await authRes.json()).result;
            if (!uid) return { status: "error", message: "Odoo auth failed" };

            const installRes = await fetch(url + "/jsonrpc", {
                method: "POST", headers: {"Content-Type":"application/json"},
                body: JSON.stringify({jsonrpc:"2.0",method:"call",id:2,
                    params:{service:"object",method:"execute_kw",
                    args:[server==="community"?"odoo_community":"odoo_enterprise",uid,"admin",
                    "ir.module.module","button_immediate_install",[[["name","=",module_name]]],{}]}})
            });
            return { status: "ok", result: (await installRes.json()).result };
        } catch(e) { return { status: "error", message: e.message }; }
    });

    app.get("/odoo/status", async (req) => {
        let ent = false, com = false;
        try { const r = await fetch("http://10.10.10.20:8069/web/login"); ent = r.status === 200; } catch {}
        try { const r = await fetch("http://10.10.10.20:8070/web/login"); com = r.status === 200; } catch {}
        return { data: { enterprise: ent, community: com } };
    });
}
module.exports = odooModuleRoutes;
