import express from "express";
import rateLimit from "express-rate-limit";

// Routes
import authRoutes from "$routes/auth/auth.routes.js";
import userRoutes from "$routes/user/user.routes.js";
import roleRoutes from "$routes/role/role.routes.js";
import filesRoutes from "$routes/file/file.routes.js";
import tadbirRoutes from "$routes/tadbir/tadbir.routes.js";
import permissionRoutes from "$routes/permission/permission.routes.js";
import profileRoutes from "$routes/profile/profile.routes.js";
import transactionRoutes from "$routes/transaction/transaction.routes.js";
import requestRoutes from "$routes/request/request.routes.js";
import curlRoutes from "$routes/curl/curl.routes.js";
import userInfoRoutes from "$routes/userInfo/userInfo.routes.js";
import paymentRoutes from "$routes/payment/payment.routes.js";
import newsRoutes from "$routes/news/news.routes.js";
import messageRoutes from "$routes/message/message.routes.js";
import certificateRoutes from "$routes/certificate/certificate.routes.js";
import paymentCardRoutes from "$routes/paymentCard/paymentCard.routes.js";
import paymentBatchCardRoutes from "$routes/paymentBatchCard/paymentBatchCard.routes.js";
import paymentSlipRoutes from "$routes/paymentSlip/paymentSlip.routes.js";
import bankRoutes from "$routes/bank/bank.routes.js";
import clubRoutes from "$routes/club/club.routes.js";
import botRoutes from "$routes/bot/bot.routes.js";
import { telegramBotConfig } from "$root/bot/index.js";

// Middlewares
import { jwt } from "$middlewares/index.js";

const appRouter = express();

// Global Rate Limiter
const globalLimiter = rateLimit({
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100, // Limit each IP to 100 requests per windowMs
  message: "Too many requests from this IP, please try again after 15 minutes",
});

// Stricter Login Rate Limiter
const loginLimiter = rateLimit({
  windowMs: 60 * 60 * 1000, // 1 hour
  max: 5, // Limit each IP to 5 login attempts per hour
  message:
    "Too many login attempts from this IP, please try again after an hour",
});

// appRouter.use(globalLimiter);

// appRouter.use("/auth", loginLimiter, authRoutes);
appRouter.use("/auth", authRoutes);
appRouter.use("/users", jwt, userRoutes);
appRouter.use("/roles", jwt, roleRoutes);
appRouter.use("/permissions", jwt, permissionRoutes);
appRouter.use("/files", jwt, filesRoutes);
appRouter.use("/tadbir", tadbirRoutes);
appRouter.use("/curl", curlRoutes);
appRouter.use("/profile", jwt, profileRoutes);
appRouter.use("/transactions", jwt, transactionRoutes);
appRouter.use("/requests", jwt, requestRoutes);
appRouter.use("/user-info", jwt, userInfoRoutes);
appRouter.use("/news", newsRoutes);
appRouter.use("/messages", messageRoutes);
appRouter.use("/certificates", jwt, certificateRoutes);
appRouter.use("/payment", paymentRoutes);
appRouter.use("/payment-cards", jwt, paymentCardRoutes);
appRouter.use("/payment-batch-cards", jwt, paymentBatchCardRoutes);
appRouter.use("/payment-slips", jwt, paymentSlipRoutes);
appRouter.use("/banks", jwt, bankRoutes);
appRouter.use("/club", jwt, clubRoutes);
if (telegramBotConfig.enabled) {
  appRouter.use("/bot", botRoutes);
}

export default appRouter;
