import axios from 'axios';
import { API_URL } from '../../../../utils/Constant';

const config = {
    headers: {
      Authorization: localStorage.getItem('token')
    }
};

const fetchProfile = async () => {
    try {
        const response = await axios.get(`${API_URL}/getMe`, config);
        return response.data;
    } catch (error) {
        throw error;
    }
};

const updateProfile = async (id, formData) => {
  
    const cfg = {
      headers: {
        'Content-Type': 'multipart/form-data',
        Authorization: localStorage.getItem('token')
      },
    };
    
    try {
        const response = await axios.put(`${API_URL}/user/update/${id}`, formData, cfg);
        return response.data;
    } catch (error) {
        throw error;
    }
};

const updatePassword = async (userId, passwordData) => {
    const cfg = {
        headers: {
            Authorization: localStorage.getItem('token'),
            'Content-Type': 'application/json',
        },
    };
    try {
      const response = await axios.put(`${API_URL}/user/update/password/${userId}`, passwordData, cfg);
      return response.data;
    } catch (error) {
        throw error;
    }
};

export default {
    fetchProfile,
    updateProfile,
    updatePassword
};
