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

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;
