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

interface WebVersionState {
  appVersion: string;
}

const initialState: WebVersionState = {
  appVersion: process.env.NEXT_PUBLIC_APP_VERSION ?? "",
};

const webVersionSlice = createSlice({
  name: "webVersionSlice",
  initialState,
  reducers: {
    setWebVersion(state, action: PayloadAction<string>) {
      state.appVersion = action.payload;
    },
    incrementBuild(state) {
      // Versionsnummer zerlegen und Build hochzählen
      const parts = state.appVersion.split(".");
      if (parts.length === 3) {
        parts[2] = (parseInt(parts[2]) + 1).toString(); // Build hochzählen
        state.appVersion = parts.join(".");
      }
    },
  },
});

export const { setWebVersion, incrementBuild } = webVersionSlice.actions;
export default webVersionSlice.reducer;
