import Router from "koa-router";
import authController from "../controllers/auth";
import protectedController from "../controllers/protected";
import categoriesController from "../controllers/categories";
import pushController from "../controllers/push";

const router = new Router({ prefix: "/api" });

/*
    Middleware requires authentication for all routes.
    Exceptions are defined in the auth middleware `backend/src/middleware/auth.ts`
    There are defined exceptions for auth routes
*/

// --- Public routes ---
// Auth routes
router.post("/auth/register", authController.register);
router.post("/auth/register-admin", authController.registerAdmin);
router.post("/auth/login", authController.login);

// --- All below routes require authentication ----
// Protected routes
router.get("/protected", protectedController.getProtectedData);

// Transaction routes
router.get("/transactions", protectedController.getUserTransactions);
router.post("/transactions", protectedController.addTransaction);

// Category routes
router.get("/categories", categoriesController.getUserCategories);
router.post("/categories", categoriesController.addCategory);
router.put("/categories/:id", categoriesController.updateCategory);

// Admin-only category routes
router.get(
  "/admin/categories",
  categoriesController.requireAdmin,
  categoriesController.getAllCategories,
);
router.post(
  "/admin/categories",
  categoriesController.requireAdmin,
  categoriesController.createGlobalCategory,
);
router.put(
  "/admin/categories/:id",
  categoriesController.requireAdmin,
  categoriesController.updateAnyCategory,
);
router.delete(
  "/admin/categories/:id",
  categoriesController.requireAdmin,
  categoriesController.deleteCategory,
);

// Push notification routes
router.get("/push/vapid-public-key", pushController.getVapidPublicKey);
router.post("/push/register", pushController.registerPushSubscription);

export default router;
