import { apiClient } from "./client";
import type { SessionUser } from "../types";

export type UserProfile = SessionUser;

export type ProfileUpdatePayload = {
  name?: string;
  email?: string;
};

export const getProfile = async (): Promise<UserProfile> => {
  const response = await apiClient.get<UserProfile>("/users/me");
  return response.data;
};

export const updateProfile = async (payload: ProfileUpdatePayload): Promise<UserProfile> => {
  const response = await apiClient.put<UserProfile>("/users/me", payload);
  return response.data;
};

export const uploadAvatar = async (file: File): Promise<UserProfile> => {
  const formData = new FormData();
  formData.append("file", file);
  const response = await apiClient.post<UserProfile>("/users/me/avatar", formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
  return response.data;
};
