import { Box, Button, Divider, Heading } from "@chakra-ui/react";
import { useNavigate, useParams } from "react-router-dom";
import { useContext, useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import GlobalStateContext from "../../../Contexts/GlobalStateContext";
import { v4 as uuidv4 } from "uuid";
import { OPACITY_ON_LOAD } from "../../../Layout/animations";
import FormInputMain from "../../../Components/FormInputMain";
import { addInvestmentType } from "./AddInvestmentType";
import { ArrowBackIcon } from "@chakra-ui/icons";
import FormInputView from "../../../Components/FormInputView";

const EditInvestmentType = () => {
  const navigate = useNavigate()
  const params = useParams();
  const { investmentType, setInvestmentType } = useContext(GlobalStateContext); 
  const [foundObject, setFoundObject] = useState(null);
  const {
    control,
    handleSubmit,
    reset,
    getValues,
    formState: { errors },
  } = useForm({
    resolver: yupResolver(addInvestmentType),
  });

  useEffect(() => {
    const id = params?.id;

    // Ensure id is compared correctly
    const found = investmentType.find((item) => item?.id.toString() === id.toString());
    setFoundObject(found);

    if (found) {
      reset({
        investmentName: found.investmentName,
        investmentNameArabic: found.investmentNameArabic,
        description: found.description,
        descriptionArabic: found.descriptionArabic,
      });
    }
  }, [params, investmentType, reset]);





  if (!foundObject) {
    return <Box>Loading...</Box>; // or any loading indicator
  }


  const formFields = [
    {
      label: "Investment name",
      value: foundObject.investmentName,
      name: "investmentName",
      type: "text",
      isRequired: true,
      section: "Personal Details",
      maxLength:50
    },
    {
      label: "Investment Name (Arabic)",
      value: foundObject.investmentNameArabic,
      name: "investmentNameArabic",
      type: "text",
      isRequired: true,
      arabic: true,
      section: "Personal Details",
    },
    {
      label: "Description",
      value: foundObject.description,
      name: "description",
      type: "textarea",
      isRequired: true,
      section: "Personal Details",
      maxLength:255
    },
    {
      label: "Description Arabic",
      value: foundObject.descriptionArabic,
      name: "descriptionArabic",
      type: "textarea",
      isRequired: true,
      arabic: true,
      section: "Personal Details",
    },
  ];

  const groupedFields = formFields?.reduce((groups, field) => {
    const { section } = field;
    if (!groups[section]) {
      groups[section] = [];
    }
    groups[section].push(field);
    return groups;
  }, {});

  const onSubmit = (data) => {
    setInvestmentType([
      {
        ...data,
        createdAt: new Date().toISOString(),
      },
      ...investmentType,
    ]);
    navigate("/investment-type");
  }

  return (
    
    <Box {...OPACITY_ON_LOAD} overflowY={"scroll"} height={"100vh"} pb={14}>
      <span
        onClick={() => navigate(-1)}
        style={{ fontSize: "15px", cursor: "pointer" }}
      >
        <ArrowBackIcon cursor={"pointer"} /> Back
      </span>
      <FormInputMain
      groupedFields={groupedFields}
      control={control}
      errors={errors}

      onSubmit={handleSubmit(onSubmit)}
    />
    </Box>
  );
};

export default EditInvestmentType;
