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 getHomeBanner = createAsyncThunk(
  "home/banner",
  async (_, { rejectWithValue }) => {
    try {
      const bannerData = await GET(API.GET_HOME_BANNERS);
      return bannerData;
    } catch (error) {
      return rejectWithValue(error.message);
    }
  },
);

// Thunk for getting Add banners
export const getAddBanner = createAsyncThunk(
  "home/add-banner",
  async (_, { rejectWithValue }) => {
    try {
      const bannerData = await GET(API.GET_ADVERTISMENT_BANNERS);
      return bannerData;
    } catch (error) {
      return rejectWithValue(error.message);
    }
  },
);

// Thunk for getting Add banners
export const getCoupons = createAsyncThunk(
  "home/add-coupons",
  async ({ amount }, { rejectWithValue }) => {
    try {
      const couponData = await GET(`${API.COUPON}?amount=${amount||""}`);
      return couponData;
    } catch (error) {
      return rejectWithValue(error.message);
    }
  },
);

// Thunk for getting Add banners
export const getWhy_Choose_Us = createAsyncThunk(
  "home/add-whychooseus",
  async (_, { rejectWithValue }) => {
    try {
      const whyprintoutData = await GET(API.GET_Why_Choose_Us);
      return whyprintoutData;
    } catch (error) {
      return rejectWithValue(error.message);
    }
  },
);

// Thunk for getting Add banners
export const getNeeds = createAsyncThunk(
  "home/add-needs",
  async (_, { rejectWithValue }) => {
    try {
      const needsData = await GET(API.GET_Needs);
      return needsData;
    } catch (error) {
      return rejectWithValue(error.message);
    }
  },
);

// Thunk for getting Add banners
export const getPartners = createAsyncThunk(
  "home/add-partners",
  async (_, { rejectWithValue }) => {
    try {
      const partnersData = await GET(API.GET_Partners);
      return partnersData;
    } catch (error) {
      return rejectWithValue(error.message);
    }
  },
);

export const getOfferProduct = createAsyncThunk(
  "home/offer-product",
  async (_, { rejectWithValue }) => {
    try {
      const partnersData = await GET(API.GET_OFFER_PRODUCT);
      return partnersData;
    } catch (error) {
      return rejectWithValue(error.message);
    }
  },
);

export const getHomeShopAndWatch = createAsyncThunk(
  "home/shop-and-watch",
  async (_, { rejectWithValue }) => {
    try {
      const partnersData = await GET(API.GET_HOME_SHOP_AND_WATCH);
      return partnersData;
    } catch (error) {
      return rejectWithValue(error.message);
    }
  },
);

// Create the slice
const homeSlice = createSlice({
  name: "home",
  initialState: {
    name: "",
    image: "",
    banners: [],
    addBanners: [],
    addCoupons: [],
    addWhyChooseUs: [],
    addNeeds: [],
    addPartners: [],
    all_offer_product: [],
    all_home_shop_and_watch: [],
    error: null,
    status: "idle",
  },
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(getHomeBanner.pending, (state) => {
        state.status = "loading";
       
      })
      .addCase(getHomeBanner.fulfilled, (state, action) => {
        state.status = "succeeded";
        state.banners = action.payload?.data || [];
        
      })
      .addCase(getHomeBanner.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.payload;
       
      })

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

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

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

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

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

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

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

export default homeSlice.reducer;
