import {
  PropsWithChildren,
  createContext,
  useContext,
  useEffect,
  useState,
} from "react";
import * as SecureStore from "expo-secure-store";

type SetUpContextType = {
  isFirstTime: boolean;
  setFirstTime: () => Promise<void>;
};

type SetUpContextProviderProps = PropsWithChildren<{}>;

const SetUpContext = createContext<SetUpContextType>({
  isFirstTime: true,
  setFirstTime: async () => {},
});

export const SetUpContextProvider = ({
  children,
}: SetUpContextProviderProps) => {
  const [isFirstTime, setIsFirstTime] = useState(
    SecureStore.getItem("isFirstTime") === "false" ? false : true
  );
  const setFirstTime = async () => {
    await SecureStore.setItemAsync("isFirstTime", "false");
    setIsFirstTime(false);
  };

  const value = {
    isFirstTime,
    setFirstTime,
  };

  return (
    <SetUpContext.Provider value={value}>{children}</SetUpContext.Provider>
  );
};

export const useSetUp = () => {
  const context = useContext(SetUpContext);
  if (context === undefined) {
    throw new Error("useSetUp must be used within a SetUpContextProvider");
  }
  return context;
};
