import { createServer } from "node:http";
import { env } from "./config/env.js";
import { prisma } from "./db/prisma.js";
import { app } from "./app.js";
import { initRealtimeServer } from "./realtime.js";

const server = createServer(app);
initRealtimeServer(server);

server.listen(env.PORT, () => {
  console.log(`Chat service started on port ${env.PORT}`);
});

async function shutdown() {
  server.close();
  await prisma.$disconnect();
  process.exit(0);
}

process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
