import { listarDistribuidores } from "@/requests/CRUD/Distribuidores/listarDistribuidores";
import { salvarDistribuidor } from "@/requests/CRUD/Distribuidores/salvarDistribuidores";
import { cadastrarInversor } from "@/requests/CRUD/Inversor/cadastrarInversor";
import { listarInversores } from "@/requests/CRUD/Inversor/listarInversores";
import { cadastrarItens } from "@/requests/CRUD/KitManual/cadastroKitManual";
import { useEffect, useState } from "react";
import { toast } from "react-toastify";

export const useInversorModal = () => {
  const [showInversorModal, setShowInversorModal] = useState(false);
  const [isInversorLoading, setIsInversorLoading] = useState(false);
  const [inversores, setInversores] = useState([]);

  const handleOpenInversorModal = () => {
    setShowInversorModal(true);
  };

  const handleCloseInversorModal = () => {
    setShowInversorModal(false);
  };

  const handleInversorSubmit = async (formData: any) => {
    try {
      setIsInversorLoading(true);

      await cadastrarItens(formData);

      await carregarInversores();

      setShowInversorModal(false);
    } catch (error) {
      console.error("Erro ao salvar Inversor:", error);
      toast.error("Erro ao cadastrar Inversor");
    } finally {
      setIsInversorLoading(false);
    }
  };
  const carregarInversores = async () => {
    const res = await listarInversores();
    setInversores(res);
  };
  useEffect(() => {
    carregarInversores();
  }, []);

  return {
    showInversorModal,
    isInversorLoading,
    handleOpenInversorModal,
    handleCloseInversorModal,
    handleInversorSubmit,
    inversores,
    carregarInversores,
  };
};
