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


export const getContent = createAsyncThunk(
    'aboutus/get-about-us',
    async (_, { rejectWithValue }) => {
        try {
            const contentData = await GET(API.GET_ABOUTUS_CONTENT);
            return contentData;

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

export const getAboutUs = createAsyncThunk(
    'aboutus',
    async (_, { rejectWithValue }) => {
        try {
            const contentData = await GET(API.GET_ABOUTUS);
            return contentData;

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


// Thunk for getting Add banners
export const getAch_and_ack = createAsyncThunk(
    'aboutus/get-ach-and-ack',
    async (_, { rejectWithValue }) => {
        try {
            const ach_and_ack = await GET(API.GET_ACH_AND_ACK);
            return ach_and_ack;

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

// Thunk for getting Add banners
export const getFoundig_Farmers = createAsyncThunk(
    'home/add-what_me_do',
    async (_, { rejectWithValue }) => {
        try {
            const whatmediadoData = await GET(API.GET_FOUNDING_FARMERS);
            return whatmediadoData;

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


// Create the slice
const aboutusSlice = createSlice({
    name: "aboutus",
    initialState: {
        content:[],
        ach_and_ack:[],
        fouding_farmers:[],
         about_us:{},
        name: '',
        subname: '',
        image: '',
        error: null,
        status: 'idle',
    },
    reducers: {},
    extraReducers: (builder) => {
        builder


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

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


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

export default aboutusSlice.reducer;


