import { parseWithSchema } from "@/utils/shared";
import api from "../axios";

import { API_ENDPOINTS } from "../endpoints";
import {
  CurrentUserProfileSchema,
  IUser,
  TCurrentUser,
  TUpdateProfileCredentials,
  UserSchema,
} from "../types";
export const profileService = {
  getProfile: async () => {
    try {
      const response = (await api.get(API_ENDPOINTS.PROFILE.READ)).data;
      const userData = parseWithSchema(
        CurrentUserProfileSchema,
        response.data,
        "Profile Service: Get Profile",
      );
      return userData;
    } catch (error) {
      console.error("ERROR caught in Profile Service: Get Profile", error);
      throw error;
    }
  },
  updateProfile: async ({
    credentials,
  }: {
    credentials: TUpdateProfileCredentials;
  }): Promise<IUser> => {
    try {
      const response = (
        await api.put(API_ENDPOINTS.PROFILE.UPDATE, credentials)
      ).data;
      return parseWithSchema(
        UserSchema,
        response.data,
        "Profile Service: Update Profile",
      );
    } catch (error) {
      console.error("ERROR caught in Profile Service: Update Profile", error);
      throw error;
    }
  },
};
