import ViewLogin from "@/components/auth/viewLogin";
import ViewVerification from "@/components/auth/viewVerification";
import Text from '@/components/Text';
import ToastCustom from "@/components/toastCustom";
import { requestPermission } from "@/lib/useNotification";
import { useAuthSession } from "@/providers/AuthProvider";
import { Redirect } from "expo-router";
import { useEffect, useState } from "react";

export default function Index() {
   const [isValid, setValid] = useState(false)
   const [phone, setPhone] = useState('')
   const [otp, setOtp] = useState(0)

   async function registerNotification() {
      const permission = await requestPermission()
   }

   useEffect(() => {
      registerNotification()
   }, [])

   const { token, isLoading } = useAuthSession()
   if (isLoading) {
      return <Text>Loading...</Text>;
   }

   if (token?.current) {
      return <Redirect href="/home" />;
   }

   if (isValid) { return <ViewVerification phone={phone} otp={otp} /> }

   return (
      <>
         <ViewLogin
            onValidate={(val) => {
               setPhone(val.phone)
               setOtp(val.otp)
               setValid(true)
            }}
         />
      </>
   );
}