import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface MyRequestsFiltersState {
  searchTerm: string;
  statusFilter: string;
  priorityFilter: string;
  currentPage: number;
}

const initialState: MyRequestsFiltersState = {
  searchTerm: '',
  statusFilter: 'all',
  priorityFilter: 'all',
  currentPage: 1,
};

const myRequestsSlice = createSlice({
  name: 'myRequests',
  initialState,
  reducers: {
    setSearchTerm: (state, action: PayloadAction<string>) => {
      state.searchTerm = action.payload;
    },
    setStatusFilter: (state, action: PayloadAction<string>) => {
      state.statusFilter = action.payload;
    },
    setPriorityFilter: (state, action: PayloadAction<string>) => {
      state.priorityFilter = action.payload;
      state.currentPage = 1; // Reset to page 1 when filter changes
    },
    setCurrentPage: (state, action: PayloadAction<number>) => {
      state.currentPage = action.payload;
    },
    clearFilters: (state) => {
      state.searchTerm = '';
      state.statusFilter = 'all';
      state.priorityFilter = 'all';
      state.currentPage = 1;
    },
  },
});

export const {
  setSearchTerm,
  setStatusFilter,
  setPriorityFilter,
  setCurrentPage,
  clearFilters,
} = myRequestsSlice.actions;

export default myRequestsSlice;

