const express = require('express');
const cors = require('cors');
const session = require('express-session');

const connectDB = require('./config/db');

const authRoutes = require('./routes/authRoutes');
const departmentRoutes = require('./routes/departmentRoutes');
const employeeRoutes = require('./routes/employeeRoutes');
const positionRoutes = require('./routes/positionRoutes');
const reportRoutes = require('./routes/reportRoutes');
const protectedRoutes = require('./routes/protectedRoutes');

const app = express();

app.set('trust proxy', 1);

const corsOptions = {
  origin: 'http://localhost:5173',
  credentials: true,
};

app.use(cors(corsOptions));
app.options('*', cors(corsOptions));
app.use(express.json());
app.use(
  session({
    secret: 'change_this_to_a_long_random_secret',
    resave: false,
    saveUninitialized: false,
    cookie: {
      httpOnly: true,
      secure: process.env.NODE_ENV === 'production',
      sameSite: 'lax',
      maxAge: 1000 * 60 * 60 * 24,
    },
  })
);
connectDB();

app.use('/api/auth', authRoutes);
app.use('/api/departments', departmentRoutes);
app.use('/api/employees', employeeRoutes);
app.use('/api/positions', positionRoutes);
app.use('/api/reports', reportRoutes);
app.use('/api/protected', protectedRoutes);

const PORT =5006;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});

