

import "dotenv/config";
import { env } from "./config/env.js";
import { AppRoutes } from "./presentation/routes.js";
import { TrackingRoutes } from "./presentation/tracking/routes.js";
import { Server } from "./presentation/server.js";
import { prisma } from "./data/postgres/index.js";

async function main() {
  try {
    await prisma.$connect();
    console.log("Database connected");

    // AppRoutes.routes debe leerse ANTES de pedir el simulator para que el
    // controller singleton ya esté construido.
    const routes = AppRoutes.routes;

    const trackingController = TrackingRoutes.controllerInstance;
    if (trackingController) {
      trackingController.buildSimulator().start();
    }

    const server = new Server({
      port: env.PORT,
      routes,
    });

    await server.start();
  } catch (error) {
    console.error(" Error starting application:", error);
    process.exit(1);
  }
}

main();
