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/count */
export default createRoute(
  "get",
  ["/count"],
  async (c) => {
    const activeOnly = c.req.query("activeOnly") === "true";

    const count = await procurement.count({ activeOnly });

    const response = apiResponse.successful(
      "Catalog item count fetched successfully!",
      { count },
    );

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