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

/* GET /v1/user/users */
export default createRoute(
  "get",
  ["/users"],

  async (c) => {
    const allUsers = await users.fetchAllUsers();

    const gatedData = await Promise.all(
      allUsers.map((u) =>
        processObjectValuePerms(u.toJson(), "obj.user", c.get("user")),
      ),
    );

    const response = apiResponse.successful(
      "Users Fetched Successfully!",
      gatedData,
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({ permissions: ["user.read.other", "user.list.other"] }),
);
