// /redux/slices/database/locationDevice/locationDevicesSlice.js

import { createSlice } from "@reduxjs/toolkit";
import { fetchLocationDevicesThunk } from "../../../thunks/database/locationDevice/fetchLocationDevicesThunk";

const locationDevicesSlice = createSlice({
  name: "locationDevices",
  initialState: {
    data: [],
    status: "idle",
    error: null,
  },
  reducers: {
    clearLocationDevices: (state) => {
      state.data = [];
      state.status = "idle";
      state.error = null;
    },
  },
  extraReducers: (builder) => {
    builder
      .addCase(fetchLocationDevicesThunk.pending, (state) => {
        state.status = "loading";
      })
      .addCase(fetchLocationDevicesThunk.fulfilled, (state, action) => {
        state.status = "succeeded";
        state.data = action.payload;
      })
      .addCase(fetchLocationDevicesThunk.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.error.message;
      });
  },
});

export const { clearLocationDevices } = locationDevicesSlice.actions;

export const selectLocationDevices = (state) => state.locationDevices.data;
export const selectLocationDeviceStatus = (state) => state.locationDevices.status;

export default locationDevicesSlice.reducer;
