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/speed-profiles */
export default createRoute(
  "post",
  ["/site/:id/speed-profiles"],
  async (c) => {
    const site = await unifiSites.fetch(c.req.param("id"));

    const body = await c.req.json();
    const schema = z
      .object({
        name: z.string(),
        downloadLimitKbps: z.number().optional(),
        uploadLimitKbps: z.number().optional(),
      })
      .strict();

    const parsed = schema.parse(body);
    const profile = await unifiSites.createUserGroup(site.siteId, parsed);

    const response = apiResponse.created(
      "UniFi Speed Profile Created Successfully!",
      profile,
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({
    permissions: [
      "unifi.access",
      "unifi.site.speed-profiles",
      "unifi.site.speed-profiles.create",
    ],
  }),
);
