import { createRoute } from "../../../../../modules/api-utils/createRoute";
import { opportunities } from "../../../../../managers/opportunities";
import { procurement } from "../../../../../managers/procurement";
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/products/labor/options */
export default createRoute(
  "get",
  ["/opportunities/opportunity/:identifier/products/labor/options"],
  async (c) => {
    const identifier = c.req.param("identifier");

    await opportunities.fetchRecord(identifier);

    const laborCatalog = await procurement.fetchLaborCatalogItems();

    const response = apiResponse.successful(
      "Labor product options fetched successfully!",
      {
        defaults: {
          customerType: "corporate",
          rates: {
            corporate: 100,
            residential: 85,
          },
          cpuMultiplier: 0.5,
          quantity: 1,
        },
        options: {
          field: {
            cwCatalogId: laborCatalog.field.cwCatalogId,
            identifier: laborCatalog.field.identifier,
            name: laborCatalog.field.name,
            taxableFlag: laborCatalog.field.salesTaxable,
          },
          tech: {
            cwCatalogId: laborCatalog.tech.cwCatalogId,
            identifier: laborCatalog.tech.identifier,
            name: laborCatalog.tech.name,
            taxableFlag: laborCatalog.tech.salesTaxable,
          },
        },
      },
    );

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