import { Snackbar } from '@mui/material';
import { useSuspenseQuery } from '@tanstack/react-query';
import { createFileRoute, redirect } from '@tanstack/react-router';
import { t } from 'i18next';
import Api from '../../api/Api';
import Profile from '../../components/Profile/Profile';
import { profileSelfQueryOptions } from '../../queries/profileQuery';
import { ROUTES } from '../../types/Routes';

const ProfilePage = () => {
  const { data: profileQuery, isFetching } = useSuspenseQuery(profileSelfQueryOptions);

  return (
    <>
      <Snackbar open={isFetching} message={t('Updating')} />
      <Profile user={profileQuery} canEdit={true} />
    </>
  );
};

export const Route = createFileRoute(`${ROUTES.PROFILE}/`)({
  loader: ({ context: { queryClient } }) => queryClient.ensureQueryData(profileSelfQueryOptions),
  beforeLoad: () => {
    if (!Api.hasAuth()) throw redirect({ to: ROUTES.INDEX });
  },
  component: ProfilePage,
});
