import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import API from "../api";

const baseURL = process.env.NEXT_PUBLIC_BASE_URL;

export const getCustomers = createAsyncThunk(
  "get/customers",
  async (_, thunkAPI) => {
    try {
      const response: any = await API.get(
        `${baseURL}/customer/dropdown`
      );
      if (response.data === undefined || typeof response.data !== "object") {
        return thunkAPI.rejectWithValue("Invalid data received");
      }
      console.log(response, "$$")
      return response?.data;
    } catch (error: any) {
      console.log(error);
      const message =
        (error.response && error.response.data && error.response.data?.error) ||
        error.message ||
        error.toString();
      return thunkAPI.rejectWithValue(message);
    }
  }
);
export const getOrderId = createAsyncThunk(
  "get/orderid",
  async (_, thunkAPI) => {
    try {
      const response: any = await API.get(
        `${baseURL}/order/generate_order_id_based_on_format`
      );
      if (response.data === undefined || typeof response.data !== "object") {
        return thunkAPI.rejectWithValue("Invalid data received");
      }
      console.log(response, "$$")
      return response?.data;
    } catch (error: any) {
      console.log(error);
      const message =
        (error.response && error.response.data && error.response.data?.error) ||
        error.message ||
        error.toString();
      return thunkAPI.rejectWithValue(message);
    }
  }
);
interface infoCustomerState {
  isGetCustomerSuccess: boolean;
  customers: any | null;
  isGetCustomerError: string | undefined;
  isGetCustomerLoading: boolean;

  // order Id type
  isOderIdSuccess: boolean;
  oderIdData: any | null;
  isOderIdError: string | undefined;
  isOderIdLoading: boolean;
}
const initialState: infoCustomerState = {
  isGetCustomerSuccess: false,
  customers: null,
  isGetCustomerError: "",
  isGetCustomerLoading: false,
  // oder ID state
  isOderIdSuccess: false,
  oderIdData: null,
  isOderIdError: "",
  isOderIdLoading: false

};
const customerInfoSlice = createSlice({
  name: "customerInfo",
  initialState,
  reducers: {
    resetGetCustomer: (state: infoCustomerState) => {
      state.isGetCustomerSuccess = false;
      state.customers = null;
      state.isGetCustomerError = "";
      state.isGetCustomerLoading = false;
    },
    resetGetOderId: (state) => {
      state.isOderIdSuccess = false;
      state.oderIdData = null;
      state.isOderIdError = ""
      state.isOderIdLoading = false

    }
  },
  extraReducers: (builder) => {
    builder
      .addCase(getCustomers.pending, (state: infoCustomerState) => {
        state.isGetCustomerLoading = true;
        state.isGetCustomerError = "";
        state.customers = null;
        state.isGetCustomerSuccess = false;
      })
      .addCase(
        getCustomers.fulfilled,
        (state: infoCustomerState, action: ReturnType<typeof getCustomers.fulfilled>) => {
          state.isGetCustomerLoading = false;
          state.customers = action?.payload?.data;
          state.isGetCustomerSuccess = true;
          state.isGetCustomerError = "";

        })
      .addCase(getCustomers.rejected, (state: infoCustomerState, action: any) => {
        state.isGetCustomerLoading = false;
        state.isGetCustomerError = action?.payload || "An error occurred";
        state.isGetCustomerSuccess = false;
        state.customers = null; // Reset user in case of rejection
      })
      .addCase(getOrderId.pending, (state: infoCustomerState) => {
        state.isOderIdLoading = true;
        state.isOderIdError = "";
        state.oderIdData = null;
        state.isOderIdSuccess = false;
      })
      .addCase(
        getOrderId.fulfilled,
        (state: infoCustomerState, action: ReturnType<typeof getCustomers.fulfilled>) => {
          state.isOderIdLoading = false;
          state.isOderIdError = "";
          state.oderIdData = action.payload.data
          state.isOderIdSuccess = true;


        }
      )
      .addCase(getOrderId.rejected, (state: infoCustomerState, action: any) => {
        state.isOderIdLoading = false;
        state.isOderIdError = action?.payload || "An error occurred";
        state.oderIdData = null;
        state.isOderIdSuccess = false;
      })
  },
});

export const { resetGetCustomer, resetGetOderId } = customerInfoSlice.actions;

export default customerInfoSlice.reducer;




