import { observer } from "mobx-react";
import { usePathname, useSearchParams } from "next/navigation";
import type { ReactNode } from "react";
import useSWR from "swr";
// components
import { LogoSpinner } from "@/components/common/logo-spinner";
// helpers
import { EPageTypes } from "@/helpers/authentication.helper";
// hooks
import { useWorkspace } from "@/hooks/store/use-workspace";
import { useUser, useUserProfile, useUserSettings } from "@/hooks/store/user";
import { useAppRouter } from "@/hooks/use-app-router";

type TPageType = EPageTypes;

type TAuthenticationWrapper = {
  children: ReactNode;
  pageType?: TPageType;
};

const isValidURL = (url: string): boolean => {
  const disallowedSchemes = /^(https?|ftp):\/\//i;
  return !disallowedSchemes.test(url);
};

export const AuthenticationWrapper = observer(function AuthenticationWrapper(
  props: TAuthenticationWrapper,
) {
  const pathname = usePathname();
  const router = useAppRouter();
  const searchParams = useSearchParams();
  const nextPath = searchParams.get("next_path");
  // props
  const { children, pageType = EPageTypes.AUTHENTICATED } = props;
  // hooks
  const {
    isLoading: isUserLoading,
    data: currentUser,
    fetchCurrentUser,
  } = useUser();
  const { data: currentUserProfile } = useUserProfile();
  const { data: currentUserSettings } = useUserSettings();
  const { loader: workspacesLoader, workspaces } = useWorkspace();

  const { isLoading: isUserSWRLoading } = useSWR(
    "USER_INFORMATION",
    async () => await fetchCurrentUser(),
    {
      revalidateOnFocus: false,
      shouldRetryOnError: false,
    },
  );

  const isUserOnboard =
    currentUserProfile?.is_onboarded ||
    (currentUserProfile?.onboarding_step?.profile_complete &&
      currentUserProfile?.onboarding_step?.workspace_create &&
      currentUserProfile?.onboarding_step?.workspace_invite &&
      currentUserProfile?.onboarding_step?.workspace_join) ||
    false;

  // const getWorkspaceRedirectionUrl = (): string => {
  //   let redirectionRoute = "/create-workspace";

  //   // validating the nextPath from the router query
  //   if (nextPath && isValidURL(nextPath.toString())) {
  //     redirectionRoute = nextPath.toString();
  //     return redirectionRoute;
  //   }

  //   // validate the last and fallback workspace_slug
  //   const currentWorkspaceSlug =
  //     currentUserSettings?.workspace?.last_workspace_slug ||
  //     currentUserSettings?.workspace?.fallback_workspace_slug;

  //   // validate the current workspace_slug is available in the user's workspace list
  //   const isCurrentWorkspaceValid = Object.values(workspaces || {}).findIndex(
  //     (workspace) => workspace.slug === currentWorkspaceSlug,
  //   );

  //   if (isCurrentWorkspaceValid >= 0)
  //     redirectionRoute = `/${currentWorkspaceSlug}`;

  //   return redirectionRoute;
  // };
  const getWorkspaceRedirectionUrl = (): string => {
    let redirectionRoute = "/create-workspace";

    // validating the nextPath from the router query
    if (nextPath && isValidURL(nextPath.toString())) {
      return nextPath.toString();
    }

    const lastWorkspaceId =
      currentUserSettings?.workspace?.last_workspace_id ||
      currentUserSettings?.workspace?.fallback_workspace_id;

    const matchedWorkspace = Object.values(workspaces || {}).find(
      (workspace) => workspace.id === lastWorkspaceId,
    );

    if (matchedWorkspace?.slug) {
      redirectionRoute = `/${matchedWorkspace.slug}`;
    } else {
      // no recorded last workspace, or it's stale — fall back to any
      // workspace the user is actually a member of
      const anyWorkspace = Object.values(workspaces || {})[0];
      if (anyWorkspace?.slug) redirectionRoute = `/${anyWorkspace.slug}`;
    }

    return redirectionRoute;
  };

  const isProfileLoading = !!currentUser?.id && !currentUserProfile?.id;

  if (
    isUserSWRLoading ||
    isUserLoading ||
    workspacesLoader ||
    isProfileLoading
  )
    return (
      <div className="relative flex h-screen w-full items-center justify-center">
        <LogoSpinner />
      </div>
    );

  if (pageType === EPageTypes.PUBLIC) return <>{children}</>;

  if (pageType === EPageTypes.NON_AUTHENTICATED) {
    if (!currentUser?.id) return <>{children}</>;
    else {
      if (currentUserProfile?.id && isUserOnboard) {
        const currentRedirectRoute = getWorkspaceRedirectionUrl();
        router.push(currentRedirectRoute);
        return <></>;
      } else {
        router.push("/onboarding");
        return <></>;
      }
    }
  }

  if (pageType === EPageTypes.ONBOARDING) {
    if (!currentUser?.id) {
      router.push(`/${pathname ? `?next_path=${pathname}` : ``}`);
      return <></>;
    } else {
      if (currentUser && currentUserProfile?.id && isUserOnboard) {
        const currentRedirectRoute = getWorkspaceRedirectionUrl();
        router.replace(currentRedirectRoute);
        return <></>;
      } else return <>{children}</>;
    }
  }

  if (pageType === EPageTypes.SET_PASSWORD) {
    if (!currentUser?.id) {
      router.push(`/${pathname ? `?next_path=${pathname}` : ``}`);
      return <></>;
    } else {
      if (
        currentUser &&
        !currentUser?.is_password_autoset &&
        currentUserProfile?.id &&
        isUserOnboard
      ) {
        const currentRedirectRoute = getWorkspaceRedirectionUrl();
        router.push(currentRedirectRoute);
        return <></>;
      } else return <>{children}</>;
    }
  }

  if (pageType === EPageTypes.AUTHENTICATED) {
    if (currentUser?.id) {
      if (currentUserProfile && currentUserProfile?.id && isUserOnboard)
        return <>{children}</>;
      else {
        router.push(`/onboarding`);
        return <></>;
      }
    } else {
      router.push(`/${pathname ? `?next_path=${pathname}` : ``}`);
      return <></>;
    }
  }

  return <>{children}</>;
});
