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

/* GET /v1/credential/credentials/:id/secure-values/:fieldId */
export default createRoute(
  "get",
  ["/credentials/:id/secure-values/:fieldId"],

  async (c) => {
    const credential = await credentials.fetch(c.req.param("id"));
    const fieldId = c.req.param("fieldId");
    const value = await credential.readSecureFieldValue(fieldId);

    const response = apiResponse.successful(
      "Secure Value Fetched Successfully!",
      { fieldId, value },
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({
    permissions: ["credential.fetch", "credential.secure_values.read"],
  }),
);
