import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { GET, POST } from "../../utils/api";
import { API } from "../../endpoints/common_endpoints";


// Thunk for getting Add banners
export const getCareer = createAsyncThunk(
    'home/add-career',
    async (_, { rejectWithValue }) => {
        try {
            const careerData = await GET(API.GET_Career);
            return careerData;

        } catch (error) {
            return rejectWithValue(error.message);
        }
    }
);
export const getCareerById = createAsyncThunk(
    'home/add-careerid',
    async (id, { rejectWithValue }) => {
        try {
            const careerData = await GET(API.GET_Careers + `/${id}`);
            return careerData;

        } catch (error) {
            return rejectWithValue(error.message);
        }
    }
);


// Create the slice
const careerSlice = createSlice({
    name: "career",
    initialState: {
        name: '',
        image: '',
        description: '',
        addCareer: [],
        addCareerById: [],
        error: null,
        status: 'idle',
    },
    reducers: {},
    extraReducers: (builder) => {
        builder

            .addCase(getCareer.pending, (state) => {
                state.status = 'loading';
              
            })
            .addCase(getCareer.fulfilled, (state, action) => {
                state.status = 'succeeded';
                state.addCareer = action.payload?.data || [];
               
            })
            .addCase(getCareer.rejected, (state, action) => {
                state.status = 'failed';
                state.error = action.payload;
               
            })

            
            .addCase(getCareerById.pending, (state) => {
                state.status = 'loading';
             
            })
            .addCase(getCareerById.fulfilled, (state, action) => {
                state.status = 'succeeded';
                state.addCareerById = action.payload?.data || [];
              
            })
            .addCase(getCareerById.rejected, (state, action) => {
                state.status = 'failed';
                state.error = action.payload;
               
            });
            
    },
});

export default careerSlice.reducer;


