import app from './app.js';
import { env } from './config/env.js';
import prisma from './services/prisma.js';

async function start() {
  try {
    await prisma.$connect();
    // eslint-disable-next-line no-console
    console.log('[db] Connected to PostgreSQL');

    app.listen(env.port, () => {
      // eslint-disable-next-line no-console
      console.log(`[server] CertsNexus API running on http://localhost:${env.port}`);
    });
  } catch (err) {
    // eslint-disable-next-line no-console
    console.error('[server] Failed to start:', err);
    process.exit(1);
  }
}

start();

process.on('SIGINT', async () => {
  await prisma.$disconnect();
  process.exit(0);
});
process.on('SIGTERM', async () => {
  await prisma.$disconnect();
  process.exit(0);
});
