// /redux/slices/database/pois/poiMarkersSlice.js
import { createSlice } from "@reduxjs/toolkit";
import { fetchPoiMarkersThunk } from "../../../thunks/database/pois/fetchPoiMarkersThunk";

const initialState = {
  data: [],
  status: "idle",
  error: null,
};

const poiMarkersSlice = createSlice({
  name: "poiMarkers",
  initialState,
  reducers: {
    setPoiMarkers: (state, action) => {
      state.data = action.payload;
    },
    clearPoiMarkers: (state) => {
      state.data = [];
    },
  },
  extraReducers: (builder) => {
    builder
      .addCase(fetchPoiMarkersThunk.pending, (state) => {
        state.status = "loading";
      })
      .addCase(fetchPoiMarkersThunk.fulfilled, (state, action) => {
        state.status = "succeeded";
        state.data = action.payload;
      })
      .addCase(fetchPoiMarkersThunk.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.error.message;
      });
  },
});

export const { setPoiMarkers, clearPoiMarkers } = poiMarkersSlice.actions;

export const selectPoiMarkers = (state) => state.poiMarkers.data;
export const selectPoiMarkersStatus = (state) => state.poiMarkers.status;
export const selectPoiMarkersError = (state) => state.poiMarkers.error;

export default poiMarkersSlice.reducer;
