import { createSlice } from "@reduxjs/toolkit";

const initialState = {
  from_age: 18,
  to_age: 70,
  from_height: 4.0,
  to_height: 7.11,
  marital_status: [],
  religion: [],
  caste: [],
  sub_caste: [],
  star: [],
  occupation: [],
  annual_income: [],
  employee_type: [],
  education: [],
  state: [],
  district: [],
  diet: "",
  family_type: [],
  filter_type: "all_matches",
  page: 1,
  isPaidMember: false,
};

const filterSlice = createSlice({
  name: "filters",
  initialState,
  reducers: {
    setAge: (state, action) => {
      state.from_age = action.payload[0];
      state.to_age = action.payload[1];
    },
    setHeight: (state, action) => {
      state.from_height = action.payload[0];
      state.to_height = action.payload[1];
    },
    setMaritalStatus: (state, action) => {
      state.marital_status = Array.isArray(action.payload) ? action.payload : [action.payload];
    },
    setReligion: (state, action) => {
      state.religion = Array.isArray(action.payload) ? action.payload : [action.payload];
    },
    setCaste: (state, action) => {
      state.caste = action.payload;
    },
    setSubCaste: (state, action) => {
      state.sub_caste = action.payload;
    },

    // universal update
    updateFilter: (state, action) => {
      return { ...state, ...action.payload };
    },

    resetFilters: (state) => {
      // Reset all filters but preserve the paid member status
      return { ...initialState, isPaidMember: state.isPaidMember };
    },
    setPage: (state, action) => {
      state.page = action.payload;
    },
    setPaidMemberStatus: (state, action) => {
      state.isPaidMember = action.payload;
    },
  },
});

export const {
  setAge,
  setHeight,
  setMaritalStatus,
  setReligion,
  setCaste,
  setSubCaste,
  updateFilter,
  resetFilters,
  setPage,
  setPaidMemberStatus,
} = filterSlice.actions;

export default filterSlice.reducer;
