import { z } from "zod";
import "./index-rbwdGzy0.js";
import { p as prisma } from "./i18n-tiaWqB1b.js";
import { r as refreshOAuthTokens } from "./refreshOAuthTokens-PUToGLGL.js";
import { g as getAppKeysFromSlug } from "./getAppKeysFromSlug-JDbO8QtR.js";
import dayjs from "dayjs";
const webexAppKeysSchema = z.object({
  client_id: z.string(),
  client_secret: z.string()
});
const getWebexAppKeys = async () => {
  const appKeys = await getAppKeysFromSlug("webex");
  return webexAppKeysSchema.parse(appKeys);
};
const webexEventResultSchema = z.object({
  id: z.string(),
  webLink: z.string(),
  siteUrl: z.string(),
  password: z.string().optional().default("")
});
const webexMeetingSchema = z.object({
  start: z.date(),
  end: z.date()
});
const webexMeetingsSchema = z.object({
  items: z.array(webexMeetingSchema)
});
const webexTokenSchema = z.object({
  scope: z.literal("spark:kms meeting:schedules_read meeting:schedules_write"),
  token_type: z.literal("Bearer"),
  access_token: z.string(),
  expires_in: z.number().optional(),
  refresh_token: z.string(),
  refresh_token_expires_in: z.number(),
  expiry_date: z.number()
});
const isTokenValid = (token) => token.expiry_date < Date.now();
const webexRefreshedTokenSchema = z.object({
  scope: z.literal("spark:kms meeting:schedules_read meeting:schedules_write"),
  token_type: z.literal("Bearer"),
  access_token: z.string(),
  expires_in: z.number().optional(),
  refresh_token: z.string(),
  refresh_token_expires_in: z.number()
});
const webexAuth = (credential) => {
  const refreshAccessToken = async (refreshToken) => {
    const { client_id, client_secret } = await getWebexAppKeys();
    const response = await refreshOAuthTokens(
      async () => await fetch("https://webexapis.com/v1/access_token", {
        method: "POST",
        headers: {
          "Content-type": "application/x-www-form-urlencoded"
        },
        body: new URLSearchParams({
          grant_type: "refresh_token",
          client_id,
          client_secret,
          refresh_token: refreshToken
        })
      }),
      "webex",
      credential.userId
    );
    const responseBody = await handleWebexResponse(response, credential.id);
    if (responseBody.error) {
      if (responseBody.error === "invalid_grant") {
        return Promise.reject(new Error("Invalid grant for Cal.com webex app"));
      }
    }
    const parsedToken = webexRefreshedTokenSchema.safeParse(responseBody);
    if (!parsedToken.success) {
      return Promise.reject(new Error("Invalid refreshed tokens were returned"));
    }
    const newTokens = parsedToken.data;
    const oldCredential = await prisma.credential.findUniqueOrThrow({ where: { id: credential.id } });
    const parsedKey = webexTokenSchema.safeParse(oldCredential.key);
    if (!parsedKey.success) {
      return Promise.reject(new Error("Invalid credentials were saved in the DB"));
    }
    const key = parsedKey.data;
    key.access_token = newTokens.access_token;
    key.refresh_token = newTokens.refresh_token;
    if (newTokens.expires_in) {
      key.expiry_date = Math.round(Date.now() + newTokens.expires_in * 1e3);
    }
    await prisma.credential.update({ where: { id: credential.id }, data: { key } });
    return newTokens.access_token;
  };
  return {
    getToken: async () => {
      let credentialKey = null;
      try {
        credentialKey = webexTokenSchema.parse(credential.key);
      } catch (error) {
        return Promise.reject("Webex credential keys parsing error");
      }
      return !isTokenValid(credentialKey) ? Promise.resolve(credentialKey.access_token) : refreshAccessToken(credentialKey.refresh_token);
    }
  };
};
const WebexVideoApiAdapter = (credential) => {
  const translateEvent = (event) => {
    return {
      title: event.title,
      start: dayjs(event.startTime).utc().format(),
      end: dayjs(event.endTime).utc().format(),
      recurrence: event.recurrence,
      //Follows RFC 2445 https://www.ietf.org/rfc/rfc2445.txt, TODO check if needs conversion
      // timezone: event.organizer.timeZone, // Comment this out for now
      agenda: event.description,
      enableJoinBeforeHost: true,
      //this is true in zoom's api, do we need it here?
      invitees: event.attendees.map((attendee) => ({
        email: attendee.email
      })),
      sendEmail: true
    };
  };
  const fetchWebexApi = async (endpoint, options) => {
    const auth = webexAuth(credential);
    const accessToken = await auth.getToken();
    console.log("result of accessToken in fetchWebexApi", accessToken);
    console.log("createMeeting options in fetchWebexApi", options);
    const response = await fetch(`https://webexapis.com/v1/${endpoint}`, {
      method: "GET",
      ...options,
      headers: {
        Authorization: `Bearer ${accessToken}`,
        ...options?.headers
      }
    });
    const responseBody = await handleWebexResponse(response, credential.id);
    return responseBody;
  };
  return {
    getAvailability: async () => {
      try {
        const responseBody = await fetchWebexApi("meetings");
        const data = webexMeetingsSchema.passthrough().parse(responseBody);
        return data.items.map((meeting) => ({
          start: meeting.start,
          end: meeting.end
        }));
      } catch (err) {
        console.error(err);
        return [];
      }
    },
    createMeeting: async (event) => {
      try {
        console.log("Creating meeting", event);
        console.log("meting body", translateEvent(event));
        console.log("request body in createMeeting", JSON.stringify(translateEvent(event)));
        const response = await fetchWebexApi("meetings", {
          method: "POST",
          headers: {
            "Content-Type": "application/json"
          },
          body: JSON.stringify(translateEvent(event))
        });
        console.log("Webex create meeting response", response);
        if (response.error) {
          if (response.error === "invalid_grant") {
            await invalidateCredential(credential.id);
            return Promise.reject(new Error("Invalid grant for Cal.com webex app"));
          }
        }
        const result = webexEventResultSchema.parse(response);
        if (result.id && result.webLink) {
          return {
            type: "webex_video",
            id: result.id.toString(),
            password: result.password || "",
            url: result.webLink
          };
        }
        throw new Error(`Failed to create meeting. Response is ${JSON.stringify(result)}`);
      } catch (err) {
        console.error(err);
        throw new Error("Unexpected error");
      }
    },
    deleteMeeting: async (uid) => {
      try {
        const response = await fetchWebexApi(`meetings/${uid}`, {
          method: "DELETE"
        });
        console.log("Webex delete meeting response", response);
        if (response.error) {
          if (response.error === "invalid_grant") {
            await invalidateCredential(credential.id);
            return Promise.reject(new Error("Invalid grant for Cal.com webex app"));
          }
        }
        return Promise.resolve();
      } catch (err) {
        return Promise.reject(new Error("Failed to delete meeting"));
      }
    },
    updateMeeting: async (bookingRef, event) => {
      try {
        const response = await fetchWebexApi(`meetings/${bookingRef.uid}`, {
          method: "PATCH",
          headers: {
            "Content-Type": "application/json"
          },
          body: JSON.stringify(translateEvent(event))
        });
        if (response.error) {
          if (response.error === "invalid_grant") {
            await invalidateCredential(credential.id);
            return Promise.reject(new Error("Invalid grant for Cal.com webex app"));
          }
        }
        const result = webexEventResultSchema.parse(response);
        if (result.id && result.webLink) {
          return {
            type: "webex_video",
            id: bookingRef.meetingId,
            password: result.password || "",
            url: result.webLink
          };
        }
        throw new Error(`Failed to create meeting. Response is ${JSON.stringify(result)}`);
      } catch (err) {
        console.error(err);
        throw new Error("Unexpected error");
      }
    }
  };
};
const handleWebexResponse = async (response, credentialId) => {
  let _response = response.clone();
  const responseClone = response.clone();
  if (_response.headers.get("content-encoding") === "gzip") {
    const responseString = await response.text();
    _response = JSON.parse(responseString);
  }
  if (!response.ok || response.status < 200 && response.status >= 300) {
    const responseBody = await _response.json();
    if (response && response.status === 124 || responseBody.error === "invalid_grant") {
      await invalidateCredential(credentialId);
    }
    throw Error(response.statusText);
  }
  if (response.status === 204) {
    return;
  }
  return responseClone.json();
};
const invalidateCredential = async (credentialId) => {
  const credential = await prisma.credential.findUnique({
    where: {
      id: credentialId
    }
  });
  if (credential) {
    await prisma.credential.update({
      where: {
        id: credentialId
      },
      data: {
        invalid: true
      }
    });
  }
};
export {
  WebexVideoApiAdapter as default,
  webexMeetingSchema,
  webexMeetingsSchema
};
