"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
const env_1 = require("./config/env");
const authRoutes_1 = __importDefault(require("./routes/authRoutes"));
const userRoutes_1 = __importDefault(require("./routes/userRoutes"));
const leadRoutes_1 = __importDefault(require("./routes/leadRoutes"));
const pipelineRoutes_1 = __importDefault(require("./routes/pipelineRoutes"));
const auditRoutes_1 = __importDefault(require("./routes/auditRoutes"));
const documentRoutes_1 = __importDefault(require("./routes/documentRoutes"));
const communicationRoutes_1 = __importDefault(require("./routes/communicationRoutes"));
const fileRoutes_1 = __importDefault(require("./routes/fileRoutes"));
const app = (0, express_1.default)();
// Middleware
app.use((0, cors_1.default)({ origin: env_1.config.corsOrigin }));
app.use(express_1.default.json());
app.use(express_1.default.urlencoded({ extended: true }));
// Routes
app.use('/api/auth', authRoutes_1.default);
app.use('/api/users', userRoutes_1.default);
app.use('/api/leads', leadRoutes_1.default);
app.use('/api/pipelines', pipelineRoutes_1.default);
app.use('/api/audit', auditRoutes_1.default);
app.use('/api', documentRoutes_1.default);
app.use('/api/communications', communicationRoutes_1.default);
app.use('/api', fileRoutes_1.default);
// Health check
app.get('/health', (req, res) => {
    res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// 404 handler
app.use((req, res) => {
    res.status(404).json({ error: 'Route not found' });
});
// Error handler
app.use((err, req, res, next) => {
    console.error('Error:', err);
    res.status(500).json({ error: 'Internal server error' });
});
// Start server
const PORT = env_1.config.port;
app.listen(PORT, () => {
    console.log(`🚀 Server running on port ${PORT}`);
    console.log(`📝 Environment: ${env_1.config.nodeEnv}`);
    console.log(`🔗 CORS origin: ${env_1.config.corsOrigin}`);
});
exports.default = app;
