import { createSlice } from "@reduxjs/toolkit";
import {getDBSwitchStatus, switchDB} from "../httpServices/httpServiceDb";

const initialState = {
    dbSwitch:{},
    dbLoading: true,
    dbError: null
}

const dbSwitchSlice = createSlice({
    name: "dbSwitch",
    initialState,
    reducers:{},
    extraReducers:(builder)=>{
        // Get Database Switch status
        builder.addCase(getDBSwitchStatus.pending, (state) => {
            state.dbLoading = true;
            state.dbError = null;
        })
        builder.addCase(getDBSwitchStatus.fulfilled, (state, action) => {
            state.dbLoading = false;
            state.dbSwitch = action.payload;
        })
        builder.addCase(getDBSwitchStatus.rejected, (state, action) => {
            state.dbLoading = false;
            state.dbError = action.payload;
        });

        // Add Database Switch status
        builder.addCase(switchDB.pending, (state) => {
            state.dbLoading = true;
            state.dbError = null;
        })
        builder.addCase(switchDB.fulfilled, (state, action) => {
            state.dbLoading = false;
            state.dbSwitch = action.payload;
        })
        builder.addCase(switchDB.rejected, (state, action) => {
            state.dbLoading = false;
            state.dbError = action.payload;
        });
    }
})

export default dbSwitchSlice.reducer;