import { createSlice } from '@reduxjs/toolkit';
import { v4 as uuidv4 } from 'uuid';

const initialState = {
  name: '',
  conditions: [],
};

const filtersSlice = createSlice({
  name: 'filters',
  initialState,
  reducers: {
    changeFilterName: (state, action) => {
      state.name = action.payload;
    },

    addCondition: (state, action) => {
      const condition_group_index = state.conditions.findIndex((filter) => filter.id === action.payload);
      if (condition_group_index !== -1) {
        state.conditions[condition_group_index].base_filters.push({
          id: uuidv4(),
          compare_with: '',
          operator: 'and',
        });
      }
    },

    setCompleteFilterState: (state, action) => {
      return action.payload;
    },

    resetFilterState: () => {
      return initialState;
    },

    addConditionGroup: (state) => {
      state.conditions.push({
        id: uuidv4(),
        base_operator: 'and',
        base_filters: [
          {
            id: uuidv4(),
            compare_with: '',
            operator: 'and',
          },
        ],
      });
    },

    updateConditionGroup: (state, action) => {
      const condition_group_index = state.conditions.findIndex((filter) => filter.id === action.payload.id);
      if (condition_group_index !== -1) {
        state.conditions[condition_group_index].base_operator = action.payload.operator;
      }
    },

    deleteConditionGroup: (state, action) => {
      state.conditions = state.conditions.filter((conditionGroup) => conditionGroup.id !== action.payload);
    },

    updateConditionValues: (state, action) => {
      const condition_group_index = state.conditions.findIndex((filter) => filter.id === action.payload.group_id);

      if (condition_group_index !== -1) {
        const condition_index = state.conditions[condition_group_index].base_filters.findIndex(
          (filter) => filter.id === action.payload.values.id
        );

        if (condition_index !== -1) {
          state.conditions[condition_group_index].base_filters[condition_index] = action.payload.values;
        }
      }
    },

    deleteCondition: (state, action) => {
      const condition_group_index = state.conditions.findIndex((filter) => filter.id === action.payload.group_id);
      if (condition_group_index !== -1) {
        if (state.conditions[condition_group_index].base_filters.length === 1) {
          state.conditions = state.conditions.filter((conditionGroup) => conditionGroup.id !== action.payload.group_id);
        } else {
          state.conditions[condition_group_index].base_filters = state.conditions[
            condition_group_index
          ].base_filters.filter((condition) => condition.id !== action.payload.condition_id);
        }
      }
    },
  },
});

export const {
  changeFilterName,
  setCompleteFilterState,
  resetFilterState,
  addCondition,
  addConditionGroup,
  updateConditionGroup,
  updateConditionValues,
  deleteCondition,
  deleteConditionGroup,
} = filtersSlice.actions;
export default filtersSlice.reducer;
