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

interface DigitalInput {
  id: number;
  label: string;
  counter: number;
  flutter: number;
  invert: boolean;
  timeFilter: number;
  weighting: number;
  zaehlerAktiv: boolean;
  eingangOffline: boolean;
  status: boolean;
}

interface DigitalInputsState {
  inputs: DigitalInput[];
}

const initialState: DigitalInputsState = {
  inputs: [],
};

const digitalInputsSlice = createSlice({
  name: "digitalInputsSlice",
  initialState,
  reducers: {
    setInputs: (state, action: PayloadAction<DigitalInput[]>) => {
      state.inputs = action.payload;
    },
    updateInputStatus: (
      state,
      action: PayloadAction<{ id: number; status: boolean }>
    ) => {
      const { id, status } = action.payload;
      const input = state.inputs.find((input) => input.id === id);
      if (input) {
        input.status = status;
      }
    },
    updateInvert: (
      state,
      action: PayloadAction<{ id: number; invert: boolean }>
    ) => {
      const { id, invert } = action.payload;
      const input = state.inputs.find((input) => input.id === id);
      if (input) {
        input.invert = invert;
      }
    },
    updateLabel: (
      state,
      action: PayloadAction<{ id: number; label: string }>
    ) => {
      const { id, label } = action.payload;
      const input = state.inputs.find((input) => input.id === id);
      if (input) {
        input.label = label; // falls du label mit aktualisieren willst
      }
    },
  },
});

export const { setInputs, updateInputStatus, updateInvert, updateLabel } =
  digitalInputsSlice.actions;

export default digitalInputsSlice.reducer;
