import React from 'react';
import { ParsedUrlQuery } from 'querystring';
import getDepartments from '../src/utils/api/getDepartments';
import AuthLayout from '../src/components/layout/AuthLayout';
import Profile from '../src/components/pages/profile/Profile';
import { ProfileProps } from '../src/model/components/profile';
import withUserSSR from '../src/utils/ssr/withSSRUser';
import getTeamById from '../src/utils/api/getTeamById';
import checkNewNotifications from '../src/utils/api/checkNewNotifications';

const profile: React.FC<ProfileProps> = ({ departments, user, team }) => (
  <AuthLayout title="Perfil" mainClassName=" profilePage">
    <Profile departments={departments} user={user} team={team} />
  </AuthLayout>
);

export const getServerSideProps = withUserSSR<ProfileProps, ParsedUrlQuery>(
  async (_, user) => {
    const departments = await getDepartments();
    const team = user.team ? await getTeamById(user.team.id) : null;
    const updatedUser = await checkNewNotifications(user);

    return {
      props: {
        departments,
        user: updatedUser,
        team,
      },
    };
  },
);

export default profile;
