const express = require('express');
const path = require('path');
const botLogic = require('./botLogic');
const configManager = require('./configManager');
const app = express();
const port = 3000;

app.use(express.json());
// Serve static files
app.use(express.static(path.join(__dirname, '../')));

// Primary Routes
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, '../login.html'));
});

app.get('/admin', (req, res) => {
    res.sendFile(path.join(__dirname, '../admin.html'));
});

app.get('/dashboard', (req, res) => {
    res.sendFile(path.join(__dirname, '../dashboard.html'));
});

// Multi-tenant Config API
app.get('/api/config/:tenantId', (req, res) => {
    const config = configManager.getTenantConfig(req.params.tenantId);
    res.json(config);
});

// Mock WhatsApp Webhook
app.post('/whatsapp/webhook', async (req, res) => {
    const { from, body } = req.body;
    console.log(`Received message from ${from}: ${body}`);
    
    const response = await botLogic.handleMessage(from, body);
    
    // In a real app, you'd call Twilio/Meta API here to send the reply
    res.json({
        success: true,
        reply: response.reply,
        buttons: response.buttons || []
    });
});

app.listen(port, '0.0.0.0', () => {
    console.log(`Curio WhatsApp Mock Server running at http://0.0.0.0:${port}`);
});
