const express = require('express');
const cors = require('cors');
const { connectDB, sequelize } = require('./config/db');
const authRoutes = require('./routes/auth');

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

// Middleware
app.use(cors());
app.use(express.json()); // Body parser for JSON

// Routes
app.use('/auth', authRoutes);

app.get('/', (req, res) => {
  res.send('Hello from Express Backend!');
});

// Database Connection & Server Start
const startServer = async () => {
  await connectDB();

  // Sync models (in production, use migration instead of sync({alter: true}))
  // For dev: force: false to keep data, alter: true to update schema
  await sequelize.sync({ alter: true });
  console.log('Database synced');

  app.listen(port, '0.0.0.0', () => {
    console.log(`Backend app listening on port ${port}`);
  });
};

startServer();
