import express from 'express';
import cors from 'cors';
import { config } from 'dotenv';
import authRoutes from './routes/auth.js';
import serverRoutes from './routes/servers.js';
import { initDb } from './db/init.js';
import { startAutoShutdownService } from './services/autoshutdown.js';
import { initDiscordBot } from './services/discordBot.js';

config();

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

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

// Initialize database
initDb();

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

app.get('/api/health', (req, res) => {
  res.json({ status: 'ok' });
});

app.listen(PORT, '0.0.0.0', () => {
  console.log(`Server running on port ${PORT}`);
  
  // Start auto-shutdown service
  startAutoShutdownService();
  
  // Start Discord bot
  initDiscordBot();
});
