import Fastify from 'fastify';
import cors from '@fastify/cors';
import helmet from '@fastify/helmet';
import pino from 'pino';
import { config } from './config/index.js';
import { pool } from './data/db.js';
import { registerAuth, registerAuthRoutes } from './auth/middleware.js';
import { registerWebhookRoutes } from './api/webhooks.js';
import { registerWebhookSecretRoutes } from './api/webhook_secrets.js';
import { registerIncidentRoutes } from './api/incidents.js';
import { registerNotificationRoutes } from './api/notifications.js';

const logger = pino({ name: 'dd0c-alert', level: config.LOG_LEVEL });

const app = Fastify({ logger: true });

await app.register(cors, { origin: config.CORS_ORIGIN });
await app.register(helmet);

registerAuth(app, config.JWT_SECRET, pool);

app.get('/health', async () => ({ status: 'ok', service: 'dd0c-alert' }));

registerAuthRoutes(app, config.JWT_SECRET, pool);
registerWebhookRoutes(app);
  registerWebhookSecretRoutes(app);
registerIncidentRoutes(app);
registerNotificationRoutes(app);

try {
  await app.listen({ port: config.PORT, host: '0.0.0.0' });
  logger.info({ port: config.PORT }, 'dd0c/alert started');
} catch (err) {
  logger.fatal(err, 'Failed to start');
  process.exit(1);
}
// Build: 2026-03-01T06:43:58Z
// Build: Sun Mar  1 06:47:59 UTC 2026
// CI fix: 06:56
