const express = require('express');
const bodyParser = require('body-parser');
const { zoomRequest } = require('./zoom-api');

const app = express();
app.use(bodyParser.json());

app.post('/webhook', async (req, res) => {
  const event = req.body.event;
  const payload = req.body.payload;

  switch (event) {
    case 'sub_account_created':
    case 'sub_account_updated':
    case 'sub_account_deleted':
      const channelId = 'eyJzaWQiOiI1NWIwMWQ3MmE0Yjg0YzRkOTg3NDYwNzJkNTBhMzMzMkBjb25mZXJlbmNlLnhtcHAuem9vbS51cyJ9; // Replace this with your team chat channel ID
      const message = `Sub-account ${event.replace('sub_account_', '')}: ${payload.account.email}`;
      await zoomRequest(`/chat/channels/${channelId}/messages`, 'POST', { message });
      break;
    default:
      console.log(`Unhandled event: ${event}`);
  }

  res.status(200).end();
});

app.listen(3000, () => console.log('Server listening on port 3000'));