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 { z } from "zod";

/* POST /v1/unifi/site/:id/wlan-groups */
export default createRoute(
  "post",
  ["/site/:id/wlan-groups"],
  async (c) => {
    const site = await unifiSites.fetch(c.req.param("id"));

    const body = await c.req.json();
    const schema = z
      .object({
        name: z.string().min(1, "Name is required"),
      })
      .strict();

    const parsed = schema.parse(body);
    const group = await unifiSites.createWlanGroup(site.siteId, parsed);

    const response = apiResponse.created(
      "UniFi WLAN Group Created Successfully!",
      group,
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({
    permissions: [
      "unifi.access",
      "unifi.site.wlan-groups",
      "unifi.site.wlan-groups.create",
    ],
  }),
);
