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

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

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

export const {
  setSearchTerm,
  setStatusFilter,
  setPriorityFilter,
  setTemplateTypeFilter,
  setSortBy,
  setSortOrder,
  setCurrentPage,
  clearFilters,
} = openRequestsSlice.actions;

export default openRequestsSlice;

