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

/* GET /v1/sales/opportunity-types */
export default createRoute(
  "get",
  ["/opportunity-types"],

  async (c) => {
    const types = await prisma.opportunityType.findMany({
      where: { inactiveFlag: false },
      orderBy: { name: "asc" },
      select: { id: true, name: true, inactiveFlag: true },
    });
    const response = apiResponse.successful(
      "Opportunity Types Fetched Successfully!",
      types,
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({ permissions: ["sales.opportunity.fetch.many"] }),
);
