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

export interface OpenRequestsFiltersState {
  searchTerm: string;
  statusFilter: string;
  priorityFilter: string;
  sortBy: 'created' | 'due' | 'priority' | 'sla';
  sortOrder: 'asc' | 'desc';
  currentPage: number;
}

const initialState: OpenRequestsFiltersState = {
  searchTerm: '',
  statusFilter: 'all',
  priorityFilter: 'all',
  sortBy: 'created',
  sortOrder: 'desc',
  currentPage: 1,
};

const openRequestsSlice = createSlice({
  name: 'openRequests',
  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;
    },
    setSortBy: (state, action: PayloadAction<'created' | 'due' | 'priority' | 'sla'>) => {
      state.sortBy = action.payload;
    },
    setSortOrder: (state, action: PayloadAction<'asc' | 'desc'>) => {
      state.sortOrder = action.payload;
    },
    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,
  setSortBy,
  setSortOrder,
  setCurrentPage,
  clearFilters,
} = openRequestsSlice.actions;

export default openRequestsSlice;

