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

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

const initialState: MyRequestsFiltersState = {
  searchTerm: '',
  statusFilter: 'all',
  priorityFilter: 'all',
  templateTypeFilter: '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
    },
    setTemplateTypeFilter: (state, action: PayloadAction<string>) => {
      state.templateTypeFilter = 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.templateTypeFilter = 'all';
      state.currentPage = 1;
    },
  },
});

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

export default myRequestsSlice;

