// redux/thunks/startFirmwareUpdateThunk.ts
import { AppDispatch } from "../store";
import {
  setProgress,
  setIsUpdating,
} from "@/redux/slices/firmwareProgressSlice";

export const startFirmwareUpdateThunk =
  (slot: number) => async (dispatch: AppDispatch) => {
    dispatch(setIsUpdating(true));
    dispatch(setProgress(0));

    const totalDuration = 5 * 60 * 1000;
    const intervalMs = 1000;
    const steps = totalDuration / intervalMs;
    let currentStep = 0;

    const interval = setInterval(() => {
      currentStep++;
      const percent = Math.min((currentStep / steps) * 100, 100);
      dispatch(setProgress(percent));

      if (currentStep >= steps) {
        clearInterval(interval);
        dispatch(setProgress(100));
        setTimeout(() => {
          dispatch(setIsUpdating(false));
        }, 500);
      }
    }, intervalMs);
  };
