const express = require('express');
const exphbs = require('express-handlebars');
const path = require('path');
const helmet = require('helmet');
const app = express();
const cors = require('cors');
require('dotenv').config();
const PORT = process.env.WEB_PORT || 3350;
const IP = process.env.IP || '0.0.0.0';

app.use(cors());

// Configure Helmet with custom CSP
// app.use(helmet({
//   contentSecurityPolicy: {
//     directives: {
//       defaultSrc: ["'self'"],
//       scriptSrc: ["'self'", "'unsafe-inline'"],
//       styleSrc: ["'self'", "'unsafe-inline'"],
//       imgSrc: ["'self'", "data:"],
//       connectSrc: ["'self'", "https://api.2weekmail.test", "https://api.2weekmail.fyi"],
//       fontSrc: ["'self'"],
//       objectSrc: ["'none'"],
//       mediaSrc: ["'self'"],
//       frameSrc: ["'self'"],
//     },
//   },
// }));

// Set up Handlebars
app.engine('hbs', exphbs.engine({
    extname: '.hbs',
    defaultLayout: 'main',
    layoutsDir: path.join(__dirname, 'views/layouts'),
    partialsDir: path.join(__dirname, 'views/partials')
}));
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
app.disable('x-powered-by');

// Static files
app.use(express.static(path.join(__dirname, 'public')));

// Routes
app.get('/', (req, res) => {
    res.render('home');
});

app.get('/stats', (req, res) => {
    res.render('stats');
});

app.get('/about', (req, res) => {
    res.render('about');
});

// Auth routes (if needed)
app.get('/auth/register', (req, res) => {
    res.render('auth/register');
});

app.get('/auth/activate', (req, res) => {
    res.render('auth/activate');
});

app.get('/privacy', (req, res) => {
    res.render('policies/privacy');
});

app.get('/terms', (req, res) => {
    res.render('policies/terms');
});

const setupSwagger = require('./swagger');
setupSwagger(app);

// const { deleteExpiredMailboxes } = require('./utils/mailbox_cleanup');
// const { cleanupOrphanedMailboxes } = require('./utils/helpers');

// const cron = require('node-cron');

// cron.schedule('0 0 * * *', async () => {
//     console.log('Running mailbox cleanup job');
//   try {
//     await deleteExpiredMailboxes();
//     await cleanupOrphanedMailboxes();
//     console.log('Mailbox cleanup completed successfully');
//   } catch (error) {
//     console.error('Mailbox cleanup failed:', error);
//   }
// });

// Start server
app.listen(PORT, IP, () => {
    console.log(`Server running on port ${PORT}`);
});
