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

/* GET /v1/sales/opportunities/opportunity/:identifier/quotes */
export default createRoute(
  "get",
  ["/opportunities/opportunity/:identifier/quotes"],
  async (c) => {
    const identifier = c.req.param("identifier");
    const includeRegenData = c.req.query("includeRegenData") === "true";
    const includeRegenParams = c.req.query("includeRegenParams") === "true";

    const item = await opportunities.fetchRecord(identifier);
    const quotes = await generatedQuotes.fetchByOpportunity(item.id);

    const response = apiResponse.successful(
      "Committed quotes fetched successfully!",
      quotes.map((q) => q.toJson({ includeRegenData, includeRegenParams })),
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({ permissions: ["sales.opportunity.quote.fetch"] }),
);
