// apps/api/src/middleware/checkLeadAccess.js

const { DatabaseClient } = require('../../../../packages/db/client');
const db = new DatabaseClient();

async function checkLeadAccess(req, res, next) {
    try {
        const user = await db.get('SELECT plan FROM users WHERE id = ?', [req.user.id]);
        console.log('User plan:', user.plan); // ADD THIS
        
        if (user.plan === 'pro') {
            req.leadLimit = null;
            console.log('Pro user - no limit'); // ADD THIS
            return next();
        }
        
        req.leadLimit = 10;
        console.log('Free user - limit set to 10'); // ADD THIS
        next();
    } catch (error) {
        console.error('Lead access error:', error);
        res.status(500).json({ error: 'Internal server error' });
    }
}

module.exports = checkLeadAccess;