const db = require('../db ');  // Import the database connection

// Controller function to handle adding data to the database
exports.addVvvetData = (req, res) => {
  const { age, category, dob, email, images, name, pan, phone } = req.body;
  console.log('Request Body:', req.body); 

  // Validate the received data
  if (!age || !category || !dob || !email || !images || !name || !pan || !phone) {
    return res.status(400).json({ error: 'Please provide all required fields.' });
  }

  // Insert the data into MySQL
  const query = 'INSERT INTO vvet (age, category, dob, email, images, name, pan, phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
  
  db.query(query, [age, category, dob, email, JSON.stringify(images), name, pan, phone], (err, results) => {
    if (err) {
      console.error('Error inserting data into MySQL:', err);
      return res.status(500).json({ error: 'An error occurred while saving the data.' });
    }
    
    // Successfully inserted
    console.log('Data inserted successfully:', results);
    res.json({ message: 'Data submitted successfully', id: results.insertId });
  });
};
