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

/* GET /v1/sales/opportunities/count */
export default createRoute(
  "get",
  ["/opportunities/count"],
  async (c) => {
    const openOnly = c.req.query("openOnly") === "true";

    const count = await opportunities.count({ openOnly });

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

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