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 || 3006;

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

// Import routes
const gorgiasRoutes = require('./routes/gorgias.routes');

// Use routes
app.use('/api/gorgias', gorgiasRoutes);

// 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(`Gorgias API server running on port ${port}`);
});

module.exports = app;
