import { logger } from "./logger/index.mjs";
import { ipLimiter } from "./utils/rateLimiter.mjs";
import { aiRoute, aiRouter } from "./routes/ai.routes.mjs";
import { dictionaryRoute, dictionaryRouter } from "./routes/dictionary.routes.mjs";
import { newsletterRoute, newsletterRouter } from "./routes/newsletter.routes.mjs";
import { organizationRoute, organizationRouter } from "./routes/organization.routes.mjs";
import { projectRoute, projectRouter } from "./routes/project.routes.mjs";
import { userRoute, userRouter } from "./routes/user.routes.mjs";
import { getAuth } from "./utils/auth/getAuth.mjs";
import { getOAuth2AccessToken } from "./controllers/oAuth2.controller.mjs";
import { attachOAuthInstance, oAuth2Middleware } from "./middlewares/oAuth2.middleware.mjs";
import { logAPIRequestURL } from "./middlewares/request.middleware.mjs";
import { authMiddleware } from "./middlewares/sessionAuth.middleware.mjs";
import { eventListenerRoute, eventListenerRouter } from "./routes/eventListener.routes.mjs";
import { searchRoute, searchRouter } from "./routes/search.routes.mjs";
import { stripeRoute, stripeRouter } from "./routes/stripe.routes.mjs";
import { tagRoute, tagRouter } from "./routes/tags.routes.mjs";
import { corsOptions } from "./utils/cors.mjs";
import { connectDB } from "./utils/mongoDB/connectDB.mjs";
import { stripeWebhook } from "./webhooks/stripe.webhook.mjs";
import { intlayer, t } from "express-intlayer";
import express from "express";
import { toNodeHandler } from "better-auth/node";
import compression from "compression";
import cookieParser from "cookie-parser";
import cors from "cors";
import dotenv from "dotenv";
import helmet from "helmet";

//#region src/index.ts
const startServer = async () => {
	const app = express();
	app.disable("x-powered-by");
	app.use(helmet());
	app.set("trust proxy", 1);
	const env = app.get("env");
	logger.info(`run as ${env}`);
	dotenv.config({ path: [
		`.env.${env}.local`,
		`.env.${env}`,
		".env.local",
		".env"
	] });
	app.use(cookieParser());
	app.use(intlayer());
	app.use(/(.*)/, ipLimiter);
	const dbClient = await connectDB();
	app.post("/webhook/stripe", express.raw({ type: "application/json" }), stripeWebhook);
	app.use(compression());
	app.use(express.urlencoded({ extended: true }));
	app.use(cors(corsOptions));
	app.get("/", (_req, res) => {
		res.send(t({
			en: "Ok - locale: en",
			fr: "Ok - locale: fr",
			es: "Ok - locale: es"
		}));
	});
	const auth = getAuth(dbClient);
	app.all("/api/auth/{*rest}", toNodeHandler(auth));
	app.use(/(.*)/, authMiddleware(auth));
	app.use(/(.*)/, attachOAuthInstance);
	app.post("/oauth2/token", getOAuth2AccessToken);
	app.use(/(.*)/, oAuth2Middleware);
	app.use(express.json());
	if (env === "development") app.use(logAPIRequestURL);
	app.use(userRoute, userRouter);
	app.use(organizationRoute, organizationRouter);
	app.use(projectRoute, projectRouter);
	app.use(tagRoute, tagRouter);
	app.use(dictionaryRoute, dictionaryRouter);
	app.use(stripeRoute, stripeRouter);
	app.use(aiRoute, aiRouter);
	app.use(eventListenerRoute, eventListenerRouter);
	app.use(searchRoute, searchRouter);
	app.use(newsletterRoute, newsletterRouter);
	app.listen(process.env.PORT, () => {
		logger.info(`Listening on port ${process.env.PORT}`);
	});
};
startServer();

//#endregion
//# sourceMappingURL=index.mjs.map