import { apiClient } from "@shared/api/client";

export interface ProfileResponse {
  user: {
    id: string;
    email: string;
    role: string;
    status: string;
  };
  profile: {
    display_name: string;
    avatar_url: string | null;
  };
}

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

export async function updateProfile(payload: { display_name?: string }): Promise<ProfileResponse> {
  const { data } = await apiClient.patch<ProfileResponse>("/api/v1/users/me", payload);
  return data;
}

export async function changePassword(payload: {
  current_password: string;
  new_password: string;
}): Promise<void> {
  await apiClient.post("/api/v1/users/me/password", payload);
}

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