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/link */
export default createRoute(
  "post",
  ["/site/:id/link"],
  async (c) => {
    const siteId = c.req.param("id");
    const body = await c.req.json();
    const schema = z.object({ companyId: z.number().int() }).strict();
    const { companyId } = schema.parse(body);

    const site = await unifiSites.linkToCompany(siteId, companyId);
    const response = apiResponse.successful(
      "UniFi Site Linked to Company Successfully!",
      site,
    );
    return c.json(response, response.status as ContentfulStatusCode);
  },
  authMiddleware({ permissions: ["unifi.access", "unifi.sites.link"] }),
);
