import { db } from "@/helpers/api";

export default async function handler(req, res) {
    if (req.method !== "POST") {
        return res.status(405).json({ success: false, error: "Method not allowed" });
    }

    await db.initialize();

    const { supplier, visible } = req.body;

    if (!supplier || visible === undefined) {
        return res.status(400).json({ success: false, error: "Date lipsă" });
    }

    try {
        const [result] = await db.sequelize.query(
            `UPDATE products SET visible = ? WHERE back_supplier = ?`,
            { replacements: [visible ? 1 : 0, supplier] }
        );

        return res.status(200).json({
            success: true,
            message: `Furnizor ${supplier} este acum ${visible ? "activ" : "inactiv"}.`,
            updatedCount: result.affectedRows,
        });
    } catch (err) {
        console.error("Eroare supplier visibility:", err);
        return res.status(500).json({ success: false, error: err.message });
    }
}