const express = require('express');
const bodyParser = require('body-parser');
const XLSX = require('xlsx');
const fs = require('fs');

const app = express();
const PORT = 3000;

// Endpoint for testing server connection
app.get('/test', (req, res) => {
    res.send('Server is reachable. Connection successful.');
});



app.use(bodyParser.json());

// Endpoint to handle updating user data
app.post('/update-user', (req, res) => {
    try {
        // Parse user data from the request body
        const userData = req.body;

        // Read the existing Excel file
        const workbook = XLSX.readFile('USER_DATABASE.xlsx'); // Update with your file path
        const sheetName = workbook.SheetNames[0];
        const worksheet = workbook.Sheets[sheetName];

        // Example: Update a specific cell with new user data
        worksheet.A2.v = userData.name; // Assuming the user's name goes in cell A2

        // Write the updated workbook back to the Excel file
        XLSX.writeFile(workbook, 'USER_DATABASE.xlsx'); // Update with your file path

        // Send a success response
        res.status(200).send('User data updated successfully');
    } catch (error) {
        console.error('Error updating user data:', error);
        res.status(500).send('Error updating user data');
    }
});


// Start the server
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});
