import { useGetInterests } from "./useGetInterests";
import { useEffect, useState } from "react";
import { ApiRequestStatus } from "../../common/const/api_request_states";
import { useSetUp } from "../../common/contexts/set_up_context";
import { router } from "expo-router";
import { useChangeInterests } from "./useChangeInterests";

export type SetUpProfileFormValues = {
  interests: number[];
  birthdate: string;
};

export const useSetUpProfile = () => {
  const { interests: allCategories, requestStatus: changeInterestsRequest } = useGetInterests();
  const { interests, toogleInterest, onSubmit: onSubmitInterests, setAllInterests } = useChangeInterests();
  const [requestStatus, setRequestStatus] = useState<ApiRequestStatus>(ApiRequestStatus.LOADING);

  // const { control, handleSubmit, setValue, getValues } = useForm<SetUpProfileFormValues>();
  const { setFirstTime } = useSetUp();
  
  useEffect(() => {
    const requests = [changeInterestsRequest];
    if (requests.every((request) => request === ApiRequestStatus.SUCCESS)) {
      setAllInterests(allCategories!);
      setRequestStatus(ApiRequestStatus.SUCCESS);
    }
  }, [changeInterestsRequest]);
  

  const onSubmit = async () => {
/*     await handleSubmit((data) => {
      
    })(); */
    
    try {
      await onSubmitInterests();
      await setFirstTime();
      router.replace("/(tabs)");
    } catch (error) {
      console.log(error);
    }
  };
  return { onSubmit, toogleInterest, interests, requestStatus };
};
