import * as React from 'react';
import redirect from 'utils/redirect';
import NotFound from 'pages/404';
import { IContext } from 'typings/context';

const Home = () => <NotFound />;

/**
 *
 * Redirects for /
 *
 * If logged in user is staff
 * / -> /staff
 *
 * If logged in user has a ssn
 * / -> /[ssn]/thjonustur
 *
 * If logged in user does not have a ssn but has msisdn
 * we assume that it's an unregistered msisdn
 * / -> /oskrad/thjonustur/[subscriptionId]
 *
 */
Home.getInitialProps = (context: IContext) => {
  const { user } = context;

  if (user?.isStaff) {
    redirect(context, '/staff');
    return {};
  }

  if (user?.profileSsn) {
    redirect(context, `/${user.profileSsn}/thjonustur`);
    return {};
  }

  if (user?.msisdn) {
    redirect(context, `/oskrad/thjonusta/${user.msisdn}`);
    return {};
  }

  return {};
};

export default Home;
