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 GenericError from "../../Errors/GenericError";

/* DELETE /v1/user/users/:identifier */
export default createRoute(
  "delete",
  ["/users/:identifier"],

  async (c) => {
    const identifier = c.req.param("identifier");

    const user = await users.fetchUser({ id: identifier });
    if (!user)
      throw new GenericError({
        name: "UserNotFound",
        message: `User with identifier '${identifier}' was not found.`,
        status: 404,
      });

    const userData = user.toJson();
    await users.deleteUser(user.id);

    const response = apiResponse.successful(
      "User Deleted Successfully!",
      userData,
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({ permissions: ["user.delete.other"] }),
);
