const QRCode = require('qrcode');

module.exports = {
    generateAssetQR
};

/**
 * Generates a QR code for an asset
 * @param {number|string} id - The asset ID
 * @param {string} baseUrl - The base URL of the frontend application
 * @returns {Promise<string>} - A Data URL of the QR code image
 */
async function generateAssetQR(id, baseUrl = 'http://localhost:3000') {
    if (!id) throw new Error('Asset ID is required');

    // Content: URL to the asset details page
    // Pattern: /apps/assets/view/[id]
    const content = `${baseUrl}/apps/assets/view/${id}`;

    try {
        const qrDataUrl = await QRCode.toDataURL(content, {
            errorCorrectionLevel: 'H',
            type: 'image/png',
            margin: 1,
            color: {
                dark: '#000000',
                light: '#ffffff'
            }
        });
        return qrDataUrl;
    } catch (err) {
        console.error('QR Code generation failed:', err);
        throw new Error('Failed to generate QR code');
    }
}
