/*
 * File: dashboardSlice.ts
 * Description: Redux slice for Dashboard state management
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */

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

// Sample case type
export interface Case {
  id: string;
  patientName: string;
  status: 'critical' | 'urgent' | 'routine';
  aiConfidence: number;
  description: string;
}

// Dashboard state type
interface DashboardState {
  caseQueue: Case[];
  loading: boolean;
  error: string | null;
  patientData:[]
}

const initialState: DashboardState = {
  caseQueue: [],
  loading: false,
  error: null,
  patientData:[]
};

/**
 * Dashboard slice for managing case queue and dashboard state
 */
const dashboardSlice = createSlice({
  name: 'dashboard',
  initialState,
  reducers: {
    fetchCasesStart(state) {
      state.loading = true;
      state.error = null;
    },
    fetchCasesSuccess(state, action: PayloadAction<Case[]>) {
      state.caseQueue = action.payload;
      state.loading = false;
    },
    fetchCasesFailure(state, action: PayloadAction<string>) {
      state.loading = false;
      state.error = action.payload;
    },
    clearCases(state) {
      state.caseQueue = [];
    },
    fetchPatientsSuccess(state, action: PayloadAction<[]>) {
      const caseTypes: ('Critical' | 'Emergency' | 'Routine')[] = ['Critical', 'Emergency', 'Routine'];
  
      // Function to get random case type
      const getRandomCaseType = (): 'Critical' | 'Emergency' | 'Routine' => {
        return caseTypes[Math.floor(Math.random() * caseTypes.length)];
      };
      
      // Add type attribute to each patient
      const patientsWithType :any = action.payload.map((patient: any) => ({
        ...patient,
        type: getRandomCaseType()
      }));
      
      state.loading = false;
      state.patientData = patientsWithType;
    }
  },
});

export const {
  fetchCasesStart,
  fetchCasesSuccess,
  fetchCasesFailure,
  clearCases,
  fetchPatientsSuccess
} = dashboardSlice.actions;

export default dashboardSlice.reducer;

/*
 * End of File: dashboardSlice.ts
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */ 