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

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

const initialState: ClosedRequestsFiltersState = {
  searchTerm: '',
  statusFilter: 'all',
  priorityFilter: 'all',
  templateTypeFilter: 'all',
  form16FinancialYear: '',
  form16Quarter: '',
  sortBy: 'created',
  sortOrder: 'desc',
  currentPage: 1,
};

const closedRequestsSlice = createSlice({
  name: 'closedRequests',
  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;
    },
    setForm16FinancialYear: (state, action: PayloadAction<string>) => {
      state.form16FinancialYear = action.payload;
    },
    setForm16Quarter: (state, action: PayloadAction<string>) => {
      state.form16Quarter = action.payload;
    },
    setSortBy: (state, action: PayloadAction<'created' | 'due' | 'priority'>) => {
      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.form16FinancialYear = '';
      state.form16Quarter = '';
      state.currentPage = 1;
    },
  },
});

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

export default closedRequestsSlice;

