import { BmsState } from "@/constants/types";
import { createSlice, PayloadAction, createAsyncThunk } from "@reduxjs/toolkit";

interface TelemetryState {
  SoH: number | null;
  SoC: number | null;
  chargingState: BmsState | null;
  lat: number | null;
  lon: number | null;
  loading: boolean;
  error: string | null;
  totalDistance: number | null;
}

const initialState: TelemetryState = {
  SoH: null,
  SoC: null,
  chargingState: null,
  lat: null,
  lon: null,
  loading: false,
  error: null,
  totalDistance: null,
};

export const telemetrySlice = createSlice({
  name: "telemetry",
  initialState,
  reducers: {
    updateTelemetry: (state, action: PayloadAction<TelemetryState>) => {
      return { ...state, ...action.payload };
    },
    setTelemetryLoading: (state) => {
      state.loading = true;
      state.error = null;
    },
    setTelemetryError: (state, action: PayloadAction<string>) => {
      state.loading = false;
      state.error = action.payload;
    },
  },
});

export const { updateTelemetry, setTelemetryLoading, setTelemetryError } =
  telemetrySlice.actions;
export default telemetrySlice.reducer;
