import { createRoute } from "../../modules/api-utils/createRoute";
import { apiResponse } from "../../modules/api-utils/apiResponse";
import { ContentfulStatusCode } from "hono/utils/http-status";
import { authMiddleware } from "../middleware/authorization";
import { PERMISSION_NODES } from "../../types/PermissionNodes";
import GenericError from "../../Errors/GenericError";

/* /v1/permissions/:category */
export default createRoute(
  "get",
  ["/:category"],

  async (c) => {
    const categoryKey = c.req.param(
      "category",
    ) as keyof typeof PERMISSION_NODES;

    if (!(categoryKey in PERMISSION_NODES)) {
      throw new GenericError({
        name: "NotFound",
        message: `Permission category "${categoryKey}" not found`,
        status: 404,
        cause: `Valid categories: ${Object.keys(PERMISSION_NODES).join(", ")}`,
      });
    }

    const response = apiResponse.successful(
      "Permission Category Fetched Successfully!",
      PERMISSION_NODES[categoryKey],
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({ permissions: ["role.read"] }),
);
