// hooks/useDataUpdater.js
import { useEffect } from "react";
import { useDispatch } from "react-redux";
// import type { AppDispatch } from "../redux/store";

// ✅ Thunks aus korrektem Pfad importieren
import { fetchGisLinesStatusThunk } from "../redux/thunks/webservice/fetchGisLinesStatusThunk";
import { fetchGisStationsMeasurementsThunk } from "../redux/thunks/webservice/fetchGisStationsMeasurementsThunk";
import { fetchGisStationsStaticDistrictThunk } from "../redux/thunks/webservice/fetchGisStationsStaticDistrictThunk";
import { fetchGisStationsStatusDistrictThunk } from "../redux/thunks/webservice/fetchGisStationsStatusDistrictThunk";
import { fetchGisSystemStaticThunk } from "../redux/thunks/webservice/fetchGisSystemStaticThunk";

const REFRESH_INTERVAL = parseInt(process.env.NEXT_PUBLIC_REFRESH_INTERVAL || "10000");

export default function useDataUpdater() {
  const dispatch = useDispatch();
  useEffect(() => {
    const updateAll = () => {
      dispatch(fetchGisLinesStatusThunk());
      dispatch(fetchGisStationsMeasurementsThunk());
      dispatch(fetchGisStationsStaticDistrictThunk());
      dispatch(fetchGisStationsStatusDistrictThunk());
      dispatch(fetchGisSystemStaticThunk());
    };

    updateAll(); // direkt initial einmal laden
    const interval = setInterval(updateAll, REFRESH_INTERVAL);

    return () => clearInterval(interval); // Cleanup
  }, [dispatch]);
}
