// redux/slices/systemspannung5VplusSlice.ts
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { getSystemspannung5VplusThunk } from "../thunks/getSystemspannung5VplusThunk";

type StateType = {
  DIA0: unknown[]; // alle Werte
  DIA1: unknown[]; // stündlich
  DIA2: unknown[]; // täglich
  isLoading: boolean;
  error: string | null;
};

const initialState: StateType = {
  DIA0: [],
  DIA1: [],
  DIA2: [],
  isLoading: false,
  error: null,
};

export const systemspannung5VplusSlice = createSlice({
  name: "systemspannung5Vplus",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(getSystemspannung5VplusThunk.pending, (state) => {
        state.isLoading = true;
        state.error = null;
      })
      .addCase(
        getSystemspannung5VplusThunk.fulfilled,
        (
          state,
          action: PayloadAction<{
            typ: "DIA0" | "DIA1" | "DIA2";
            data: unknown[];
          }>
        ) => {
          state.isLoading = false;
          state[action.payload.typ] = action.payload.data;
        }
      )
      .addCase(getSystemspannung5VplusThunk.rejected, (state, action) => {
        state.isLoading = false;
        state.error = action.payload as string;
      });
  },
});

export default systemspannung5VplusSlice.reducer;
