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

import { createAsyncThunk } from '@reduxjs/toolkit';
import { fetchProfileStart, fetchProfileSuccess, fetchProfileFailure } from './profileSlice';
import { profileAPI } from '../services/profileAPI';

/**
 * Thunk to fetch user profile from API
 */
export const fetchProfile = createAsyncThunk(
  'profile/fetchProfile',
  async (userId: string, { dispatch, rejectWithValue }) => {
    try {
      dispatch(fetchProfileStart());
      const response = await profileAPI.getProfile(userId);
      if (response.ok && response.data) {
        dispatch(fetchProfileSuccess(response.data));
      } else {
        dispatch(fetchProfileFailure(response.problem || 'Unknown error'));
        return rejectWithValue(response.problem || 'Unknown error');
      }
    } catch (error: any) {
      dispatch(fetchProfileFailure(error.message));
      return rejectWithValue(error.message);
    }
  }
);

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