const express = require('express');
const cors = require('cors');
const path = require('path');
require('dotenv').config({ 
  path: path.resolve(__dirname, '.env') 
});

const app = express();
const port = process.env.PORT || 3005;

app.use(cors());
app.use(express.json());

// Import routes
const campaignRoutes = require('./routes/campaigns.routes');

// Use routes
app.use('/api/meta', campaignRoutes);

// Error handling middleware
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({ error: 'Something went wrong!' });
});

// Start server
app.listen(port, () => {
  console.log(`Meta API server running on port ${port}`);
});

module.exports = app;
