/*
 * File: dashboardActions.ts
 * Description: Async actions (thunks) for Dashboard state
 * Design & Developed by Tech4Biz Solutions
 * Copyright (c) Spurrin Innovations. All rights reserved.
 */

import { createAsyncThunk } from '@reduxjs/toolkit';
import { fetchCasesStart, fetchCasesSuccess, fetchCasesFailure, fetchPatientsSuccess } from './dashboardSlice';
import { caseAPI } from '../services/caseAPI';

/**
 * Thunk to fetch case queue from API
 */
export const fetchCases = createAsyncThunk(
  'dashboard/fetchCases',
  async (_, { dispatch, rejectWithValue }) => {
    try {
      dispatch(fetchCasesStart());
      const response = await caseAPI.getCases();
      if (response.ok && response.data) {
        dispatch(fetchCasesSuccess(response.data));
      } else {
        dispatch(fetchCasesFailure(response.problem || 'Unknown error'));
        return rejectWithValue(response.problem || 'Unknown error');
      }
    } catch (error: any) {
      dispatch(fetchCasesFailure(error.message));
      return rejectWithValue(error.message);
    }
  }
);

export const fetchPatients = createAsyncThunk(
  'dashboard/fetchPatients',
  async (payload, { dispatch, rejectWithValue }) => {
    try {
      dispatch(fetchCasesStart());
      const response :any = await caseAPI.getPatients(payload);
      console.log('response i got',response)
      if (response.ok && response.data && response.data.data) {
        dispatch(fetchPatientsSuccess(response.data.data));
      } 
    } catch (error: any) {
      dispatch(fetchCasesFailure(error.message));
      return rejectWithValue(error.message);
    }
  }
);

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