import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import pool from './config/db';

import messagesRouter from './routes/messages';

dotenv.config();

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

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

app.use('/api/messages', messagesRouter);
import supportRouter from './routes/support';
app.use('/api/support', supportRouter);

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

// Database connection test
app.get('/api/test-db', async (req, res) => {
    try {
        const [rows] = await pool.query('SELECT 1 + 1 AS solution');
        res.json({ status: 'connected', result: rows });
    } catch (error: any) {
        console.error('Database connection error:', error);
        res.status(500).json({ status: 'error', message: error.message });
    }
});

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});
