require('dotenv').config();
const express = require('express');
const userRoutes = require('./routes/user.routes');

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

// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Routes
app.use('/api/users', userRoutes);

// Alapértelmezett route
app.get('/', (req, res) => {
  res.json({
    message: 'User Management API - MINTA Megoldás',
    endpoints: {
      users: {
        getAll: 'GET /api/users',
        getById: 'GET /api/users/:id',
        create: 'POST /api/users',
        update: 'PUT /api/users/:id',
        delete: 'DELETE /api/users/:id'
      }
    }
  });
});

// 404 handler
app.use((req, res) => {
  res.status(404).json({
    success: false,
    message: 'Endpoint nem található!'
  });
});

// Error handler
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({
    success: false,
    message: 'Szerver hiba történt!'
  });
});

app.listen(PORT, () => {
  console.log(`✅ Szerver fut a http://localhost:${PORT} címen`);
  console.log(`📊 Prisma Studio: npx prisma studio`);
});
