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

/* /v1/procurement/items/:identifier/refresh-inventory */
export default createRoute(
  "post",
  ["/items/:identifier/refresh-inventory"],
  async (c) => {
    const identifier = c.req.param("identifier");
    const item = await procurement.fetchItem(identifier);

    await item.refreshInventory();

    const response = apiResponse.successful(
      "Inventory refreshed successfully!",
      item.toJson(),
    );

    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({ permissions: ["procurement.catalog.inventory.refresh"] }),
);
