import { Hono } from "hono/tiny";
import { createRoute } from "../../modules/api-utils/createRoute";
import { credentialTypes } from "../../managers/credentialTypes";
import { apiResponse } from "../../modules/api-utils/apiResponse";
import { ContentfulStatusCode } from "hono/utils/http-status";
import { authMiddleware } from "../middleware/authorization";
import { processObjectValuePerms } from "../../modules/permission-utils/processObjectPermissions";

/* /v1/credential-type/:identifier */
export default createRoute(
  "get",
  ["/:identifier"],

  async (c) => {
    const credentialType = await credentialTypes.fetch(
      c.req.param("identifier"),
    );

    const gatedData = await processObjectValuePerms(
      credentialType.toJson({ includeCredentialCount: true }),
      "obj.credentialType",
      c.get("user"),
    );

    const response = apiResponse.successful(
      "Credential Type Fetched Successfully!",
      gatedData,
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({ permissions: ["credential_type.fetch"] }),
);
