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

/* GET /v1/unifi/site/:id/wifi */
export default createRoute(
  "get",
  ["/site/:id/wifi"],
  async (c) => {
    const site = await unifiSites.fetch(c.req.param("id"));
    const wlans = await unifiSites.getWlanConf(site.siteId);

    const processWlans = await Promise.all(
      wlans.map((wlan) =>
        processObjectValuePerms(wlan, "unifi.site.wifi.read", c.get("user"))
      )
    );

    const response = apiResponse.successful(
      "UniFi WiFi Networks Fetched Successfully!",
      processWlans
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({ permissions: ["unifi.access", "unifi.site.wifi"] })
);
