const express = require('express');
const router = express.Router();
const { sendEmail } = require('../utils/emailService'); // Assuming generic sendEmail exists or we use specific

router.post('/', async (req, res) => {
    const { name, email, message } = req.body;

    if (!name || !email || !message) {
        return res.status(400).json({ message: 'All fields are required' });
    }

    try {
        // Hardcoded support email as requested to prevent scraping
        const supportEmail = 'support@infoseclabs.io';
        const subject = `New Contact Form Submission from ${name}`;
        const htmlBody = `
            <h3>New Contact Request</h3>
            <p><strong>Name:</strong> ${name}</p>
            <p><strong>Email:</strong> ${email}</p>
            <p><strong>Message:</strong></p>
            <p>${message}</p>
        `;

        // Send email (using existing email service logic)
        // If emailService only has specific functions, we might need to add a generic one.
        // For now, I'll assume we can use a generic helper or add it.
        // Let's check emailService first, but I'll write this file assuming I can add/import a sender.

        // Actually, let's look at emailService.js first to be safe.
        // But for now, I'll write the controller with a placeholder and fix dependencies if needed.
        // Re-reading previous context: 'sendVerificationEmail', 'sendPasswordResetEmail', 'sendAccountLockedEmail' exist.
        // I will likely need to add 'sendContactEmail' to emailService.js

        const { sendContactEmail } = require('../utils/emailService');
        await sendContactEmail(name, email, message);

        res.json({ message: 'Message sent successfully' });
    } catch (error) {
        console.error('Contact form error:', error);
        res.status(500).json({ message: 'Server error' });
    }
});

module.exports = router;
