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";
import { z } from "zod";

/* POST /v1/procurement/items/:identifier/unlink */
export default createRoute(
  "post",
  ["/items/:identifier/unlink"],
  async (c) => {
    const identifier = c.req.param("identifier");
    const body = await c.req.json();
    const schema = z.object({ targetId: z.string() }).strict();
    const { targetId } = schema.parse(body);

    const item = await procurement.unlinkItems(identifier, targetId);

    const response = apiResponse.successful(
      "Catalog item unlinked successfully!",
      item.toJson({ includeLinkedItems: true }),
    );

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