import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import type { DateRange } from '@/services/dashboard.service';

/** Dealer All Requests: main filter to switch between All, Claim Management, Form 16. */
export type RequestTypeFilter = 'all' | 'claim_management' | 'form_16';

export interface RequestsFiltersState {
  searchTerm: string;
  statusFilter: string;
  priorityFilter: string;
  templateTypeFilter: string;
  /** Dealer only: request type for All Requests (all | claim_management | form_16). */
  requestTypeFilter: RequestTypeFilter;
  /** When template is Form 16: financial year (e.g. 2024-25) */
  form16FinancialYear: string;
  /** When template is Form 16: quarter (Q1, Q2, Q3, Q4) */
  form16Quarter: string;
  slaComplianceFilter: string;
  departmentFilter: string;
  initiatorFilter: string;
  approverFilter: string;
  approverFilterType: 'current' | 'any';
  dateRange: DateRange;
  customStartDate?: Date;
  customEndDate?: Date;
  showCustomDatePicker: boolean;
  currentPage: number;
}

const initialState: RequestsFiltersState = {
  searchTerm: '',
  statusFilter: 'all',
  priorityFilter: 'all',
  templateTypeFilter: 'all',
  requestTypeFilter: 'all',
  form16FinancialYear: 'all',
  form16Quarter: 'all',
  slaComplianceFilter: 'all',
  departmentFilter: 'all',
  initiatorFilter: 'all',
  approverFilter: 'all',
  approverFilterType: 'current',
  dateRange: 'all',
  customStartDate: undefined,
  customEndDate: undefined,
  showCustomDatePicker: false,
  currentPage: 1,
};

const requestsSlice = createSlice({
  name: 'requests',
  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;
      if (action.payload !== 'FORM_16') {
        state.form16FinancialYear = 'all';
        state.form16Quarter = 'all';
      }
    },
    setRequestTypeFilter: (state, action: PayloadAction<RequestTypeFilter>) => {
      state.requestTypeFilter = action.payload;
      if (action.payload !== 'form_16') {
        state.form16FinancialYear = 'all';
        state.form16Quarter = 'all';
      }
    },
    setForm16FinancialYear: (state, action: PayloadAction<string>) => {
      state.form16FinancialYear = action.payload;
    },
    setForm16Quarter: (state, action: PayloadAction<string>) => {
      state.form16Quarter = action.payload;
    },
    setSlaComplianceFilter: (state, action: PayloadAction<string>) => {
      state.slaComplianceFilter = action.payload;
    },
    setDepartmentFilter: (state, action: PayloadAction<string>) => {
      state.departmentFilter = action.payload;
    },
    setInitiatorFilter: (state, action: PayloadAction<string>) => {
      state.initiatorFilter = action.payload;
    },
    setApproverFilter: (state, action: PayloadAction<string>) => {
      state.approverFilter = action.payload;
    },
    setApproverFilterType: (state, action: PayloadAction<'current' | 'any'>) => {
      state.approverFilterType = action.payload;
    },
    setDateRange: (state, action: PayloadAction<DateRange>) => {
      state.dateRange = action.payload;
    },
    setCustomStartDate: (state, action: PayloadAction<Date | undefined>) => {
      state.customStartDate = action.payload;
    },
    setCustomEndDate: (state, action: PayloadAction<Date | undefined>) => {
      state.customEndDate = action.payload;
    },
    setShowCustomDatePicker: (state, action: PayloadAction<boolean>) => {
      state.showCustomDatePicker = 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.requestTypeFilter = 'all';
      state.form16FinancialYear = 'all';
      state.form16Quarter = 'all';
      state.slaComplianceFilter = 'all';
      state.departmentFilter = 'all';
      state.initiatorFilter = 'all';
      state.approverFilter = 'all';
      state.approverFilterType = 'current';
      state.dateRange = 'all';
      state.customStartDate = undefined;
      state.customEndDate = undefined;
      state.showCustomDatePicker = false;
      state.currentPage = 1;
    },
  },
});

export const {
  setSearchTerm,
  setStatusFilter,
  setPriorityFilter,
  setTemplateTypeFilter,
  setRequestTypeFilter,
  setForm16FinancialYear,
  setForm16Quarter,
  setSlaComplianceFilter,
  setDepartmentFilter,
  setInitiatorFilter,
  setApproverFilter,
  setApproverFilterType,
  setDateRange,
  setCustomStartDate,
  setCustomEndDate,
  setShowCustomDatePicker,
  setCurrentPage,
  clearFilters,
} = requestsSlice.actions;

export default requestsSlice;


