import { logger } from "@formbricks/logger";
import { TIntegrationNotionInput } from "@formbricks/types/integration/notion";
import { responses } from "@/app/lib/api/response";
import { withV1ApiWrapper } from "@/app/lib/api/with-api-logging";
import {
  ENCRYPTION_KEY,
  NOTION_OAUTH_CLIENT_ID,
  NOTION_OAUTH_CLIENT_SECRET,
  NOTION_REDIRECT_URI,
  WEBAPP_URL,
} from "@/lib/constants";
import { symmetricEncrypt } from "@/lib/crypto";
import { createOrUpdateIntegration, getIntegrationByType } from "@/lib/integration/service";
import {
  IntegrationOAuthStateError,
  consumeIntegrationOAuthState,
  getSafeOAuthCallbackError,
} from "@/lib/oauth/integration-state";
import { capturePostHogEvent } from "@/lib/posthog";
import { getOrganizationIdFromWorkspaceId } from "@/lib/utils/helper";
import { canUserWriteWorkspaceIntegrations } from "@/lib/workspace/auth";

/**
 * The callback's verified context: the workspace its OAuth state names (which the user may write
 * integrations to), the authorization code and the page to return to. Or the response that ends the
 * callback: a bad state or code, no access, or the provider's error sent back to that page.
 */
const resolveOAuthCallback = async (
  req: Request,
  userId: string
): Promise<{ response: Response } | { workspaceId: string; code: string | null; redirectUrl: URL }> => {
  const url = req.url;
  const queryParams = new URLSearchParams(url.split("?")[1]); // Split the URL and get the query parameters
  const state = queryParams.get("state");
  const code = queryParams.get("code");
  const error = queryParams.get("error");

  let oauthState;
  try {
    oauthState = await consumeIntegrationOAuthState({
      provider: "notion",
      userId: userId,
      state,
    });
  } catch (err) {
    if (err instanceof IntegrationOAuthStateError) {
      return {
        response: responses.badRequestResponse("Invalid OAuth state"),
      };
    }

    throw err;
  }

  const workspaceId = oauthState.workspaceId;
  if (!workspaceId) {
    return {
      response: responses.badRequestResponse("Invalid workspaceId"),
    };
  }

  const canUserAccessWorkspace = await canUserWriteWorkspaceIntegrations(userId, workspaceId);
  if (!canUserAccessWorkspace) {
    return {
      response: responses.unauthorizedResponse(),
    };
  }

  const basePath = `/workspaces/${workspaceId}/settings/workspace`;
  const redirectUrl = new URL(`${basePath}/integrations/notion`, WEBAPP_URL);
  const safeError = getSafeOAuthCallbackError(error);

  if (code && typeof code !== "string") {
    return {
      response: responses.badRequestResponse("`code` must be a string"),
    };
  }

  if (!code && safeError) {
    redirectUrl.searchParams.set("error", safeError);
    return {
      response: Response.redirect(redirectUrl),
    };
  }

  return { workspaceId, code, redirectUrl };
};

export const GET = withV1ApiWrapper({
  handler: async ({ req, authentication }) => {
    if (!authentication || !("user" in authentication)) {
      return { response: responses.notAuthenticatedResponse() };
    }

    const callback = await resolveOAuthCallback(req, authentication.user.id);
    if ("response" in callback) return callback;
    const { workspaceId, code, redirectUrl } = callback;

    const client_id = NOTION_OAUTH_CLIENT_ID;
    const client_secret = NOTION_OAUTH_CLIENT_SECRET;
    const redirect_uri = NOTION_REDIRECT_URI;
    if (!client_id)
      return {
        response: responses.internalServerErrorResponse("Notion client id is missing"),
      };
    if (!redirect_uri)
      return {
        response: responses.internalServerErrorResponse("Notion redirect url is missing"),
      };
    if (!client_secret)
      return {
        response: responses.internalServerErrorResponse("Notion client secret is missing"),
      };
    if (code) {
      // encode in base 64
      const encoded = Buffer.from(`${client_id}:${client_secret}`).toString("base64");

      const response = await fetch("https://api.notion.com/v1/oauth/token", {
        method: "POST",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
          Authorization: `Basic ${encoded}`,
        },
        body: JSON.stringify({
          grant_type: "authorization_code",
          code: code,
          redirect_uri: redirect_uri,
        }),
      });

      const tokenData = await response.json();
      const encryptedAccessToken = symmetricEncrypt(tokenData.access_token, ENCRYPTION_KEY);
      tokenData.access_token = encryptedAccessToken;

      const notionIntegration: TIntegrationNotionInput = {
        type: "notion" as const,
        config: {
          key: tokenData,
          data: [],
        },
      };

      const existingIntegration = await getIntegrationByType(workspaceId, "notion");
      if (existingIntegration) {
        notionIntegration.config.data = existingIntegration.config.data;
      }

      const result = await createOrUpdateIntegration(workspaceId, notionIntegration);

      if (result) {
        try {
          const organizationId = await getOrganizationIdFromWorkspaceId(workspaceId);
          capturePostHogEvent(
            authentication.user.id,
            "integration_connected",
            {
              integration_type: "notion",
              organization_id: organizationId,
              workspace_id: workspaceId,
            },
            { organizationId, workspaceId }
          );
        } catch (err) {
          logger.error({ error: err }, "Failed to capture PostHog integration_connected event for notion");
        }

        return {
          response: Response.redirect(redirectUrl),
        };
      }
    }

    return {
      response: responses.badRequestResponse("Missing code or error parameter"),
    };
  },
});
