import * as express from "express";
import AdminRoute from "./admin";
import UserRoute from "./user";
import InternalRoute from "./internal";
import ClientRouter from "./client";
import cors from "cors";
import OAuthRoute from "./oauth";
import config from "../config";
import JRPCEndpoint from "./jrpc";

const ApiRouter: express.IRouter = express.Router();
ApiRouter.use("/admin", AdminRoute);
ApiRouter.use(cors());
ApiRouter.use("/user", UserRoute);
ApiRouter.use("/internal", InternalRoute);
ApiRouter.use("/oauth", OAuthRoute);

ApiRouter.use("/client", ClientRouter);

/**
 * @api {post} /jrpc
 * @apiName InternalJRPCEndpoint
 * 
 * @apiGroup user
 * @apiPermission none
 * 
 * @apiErrorExample {Object} Error-Response:
     {
        error: [
            {
                message: "Some Error",
                field: "username"
            }
        ],
        status: 400
     }
 */
ApiRouter.post("/jrpc", JRPCEndpoint);

// Legacy reasons (deprecated)
ApiRouter.use("/", ClientRouter);

ApiRouter.get("/config.json", (req, res) => {
   return res.json({
      name: config.core.name,
      url: config.core.url,
   });
});

export default ApiRouter;
