import { useNavigate } from 'react-router-dom';
import Login from '@components/TopLevel/Login/Index';
import { login, createUser, getSelectedSeatsApi } from '../../lib/api';
import { setItem } from '../../lib/sessionStorage';

const LoginPage = () => {
  const navigate = useNavigate();

  const onSubmit = async (email) => {
    try {
      const userData = await login(email);
      if (userData?.length > 0) {
        setItem('user', userData[0].id);
      } else {
        const data = await createUser(email);
        setItem('user', data.id);
      }
      navigate('/');
    } catch (error) {
      console.error('Error during login or user creation:', error);
    }
  };

  return <Login onSubmit={onSubmit} />;
};

export default LoginPage;
