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


// Thunk for getting home banners
export const postRating = createAsyncThunk(
    'rating',
    async (data, { rejectWithValue }) => {
        try {
            const response = await POST(API.GET_ALL_REVIEWS, data
            );
            return response;

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

// Thunk for getting home banners
export const getAllRatings = createAsyncThunk(
    'all-ratings',
    async ({ product_id }, { rejectWithValue }) => {
        try {
            const data = await GET(API.GET_ALL_REVIEWS + `/${product_id}`
            );
            return data;

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

// Thunk for gettingStore
export const getAvgRatings = createAsyncThunk(
    'avg-ratings',
    async (id, { rejectWithValue }) => {
        try {
            const data = await GET(API.GET_AVG_REVIEWS + `/${id}`);
            return data;

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

// Create the slice
const ratingSlice = createSlice({
    name: "rating",
    initialState: {
        ratings: [],
        avgRatings: {},
        error: null,
        status: 'idle',
    },
    reducers: {},
    extraReducers: (builder) => {
        builder
            .addCase(postRating.pending, (state) => {
                state.status = 'loading';
               
            })
            .addCase(postRating.fulfilled, (state, action) => {
                state.status = 'succeeded';
                console.log('Response payload in fulfilled case:', action.payload);
                console.log('State after fulfilled', state);
            })
            .addCase(postRating.rejected, (state, action) => {
                state.status = 'failed';
                state.error = action.payload;
                console.log('State after rejected', state);
            })

            .addCase(getAllRatings.pending, (state) => {
                state.status = 'loading';
               
            })
            .addCase(getAllRatings.fulfilled, (state, action) => {
                state.status = 'succeeded';
                console.log('Response payload in fulfilled case:', action.payload);
                state.ratings = action.payload?.data || [];
                console.log('State after fulfilled', state);
            })
            .addCase(getAllRatings.rejected, (state, action) => {
                state.status = 'failed';
                state.error = action.payload;
                console.log('State after rejected', state);
            })

            .addCase(getAvgRatings.pending, (state) => {
                state.status = 'loading';
              
            })
            .addCase(getAvgRatings.fulfilled, (state, action) => {
                state.status = 'succeeded';
                console.log('Response payload in fulfilled case:', action.payload);
                state.avgRatings = action.payload?.data || {};
                console.log('State after fulfilled', state);
            })
            .addCase(getAvgRatings.rejected, (state, action) => {
                state.status = 'failed';
                state.error = action.payload;
                console.log('State after rejected', state);
            });
    },
});

export default ratingSlice.reducer;




