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

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

    await db.initialize();

    try {
        // Dacă cel puțin un produs e visible=1 per furnizor, îl considerăm ON
        const [rows] = await db.sequelize.query(`
            SELECT 
                back_supplier,
                SUM(CASE WHEN visible = 1 THEN 1 ELSE 0 END) as visibleCount,
                COUNT(*) as totalCount
            FROM products
            WHERE back_supplier IN ('AVEX', 'DEPOX')
            GROUP BY back_supplier
        `);

        const status = {};
        rows.forEach(row => {
            status[row.back_supplier] = row.visibleCount > 0;
        });

        return res.status(200).json({ success: true, status });
    } catch (err) {
        return res.status(500).json({ success: false, error: err.message });
    }
}