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

import { createAsyncThunk } from '@reduxjs/toolkit';
import { setLoading, selectCase, setError } from './caseReviewSlice';
import { setImages } from './dicomSlice';
import { setFindings, setConfidence } from './aiAnalysisSlice';
import { dicomAPI } from '../services/dicomAPI';
import { aiAnalysisAPI } from '../services/aiAnalysisAPI';

/**
 * Thunk to fetch case details, DICOM images, and AI analysis
 */
export const fetchCaseDetails = createAsyncThunk(
  'caseReview/fetchCaseDetails',
  async (caseId: string, { dispatch, rejectWithValue }) => {
    try {
      dispatch(setLoading(true));
      // Fetch DICOM images
      const dicomRes = await dicomAPI.getImages(caseId);
      if (dicomRes.ok && dicomRes.data) {
        dispatch(setImages(dicomRes.data));
      } else {
        dispatch(setError('Failed to load DICOM images'));
        return rejectWithValue('Failed to load DICOM images');
      }
      // Fetch AI analysis
      const aiRes = await aiAnalysisAPI.getAnalysis(caseId);
      if (aiRes.ok && aiRes.data) {
        dispatch(setFindings(aiRes.data.findings));
        dispatch(setConfidence(aiRes.data.confidence));
      } else {
        dispatch(setError('Failed to load AI analysis'));
        return rejectWithValue('Failed to load AI analysis');
      }
      dispatch(setLoading(false));
    } catch (error: any) {
      dispatch(setError(error.message));
      dispatch(setLoading(false));
      return rejectWithValue(error.message);
    }
  }
);

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