const express = require('express')
const cors = require('cors')
const session = require('express-session')
require('dotenv').config()
const {pool, dbconn} = require('./config/db')

const app = express()

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

app.use(cors({
    origin:['http://localhost:3001', 'http://localhost:5173'],
    credentials: true
}))

app.use(session({
    secret:process.env.SESSION_SECRET,
    resave:false,
    saveUninitialized:false,
    cookie:{
        secure:false,
        maxAge:1000*60*60*24
    }
}))

const authRoutes = require('./routes/authRoutes')
const employeeRoutes = require('./routes/employeeRoutes')
const departmentRoutes = require('./routes/departmentRoutes')
const salaryRoutes = require('./routes/salaryRoutes')
const reportRoutes = require('./routes/reportRoutes')

app.use('/api/auth', authRoutes)
app.use('/api/employees', employeeRoutes)
app.use('/api/departments', departmentRoutes)
app.use('/api/salaries', salaryRoutes)
app.use('/api/reports', reportRoutes)

app.get('/api/health', (req, res) => {
    res.json({ message: 'Server is running', status: 'OK' })
})
app.get('/api/test', (req, res) => {
    res.json({ message: 'API is working' })
})

const PORT = process.env.PORT || 5000
app.listen(PORT, () =>{
    console.log(`server is running on port ${PORT}`)
})
