import { apiClient } from '@/services/api/client';

export interface UserProfile {
  id: number;
  username: string;
  first_name: string | null;
  last_name: string | null;
  avatar_url: string | null;
  bio: string | null;
  location: string | null;
  birthdate: string | null;
  gender: string | null;
  created_at: string;
}

export async function getProfile(userId: number): Promise<UserProfile> {
  const response = await apiClient.get(`/users/${userId}/profile`);
  return response.data.profile;
}

export async function getProfileByUsername(username: string): Promise<UserProfile> {
  const response = await apiClient.get(`/users/by-username/${username}`);
  // Backend returns { profile: {...} }
  return response.data.profile || response.data;
}

export interface UpdateProfileRequest {
  first_name?: string;
  last_name?: string;
  username?: string;
  bio?: string;
  location?: string;
  birthdate?: string;
  gender?: string;
}

export async function updateProfile(userId: number, data: UpdateProfileRequest): Promise<UserProfile> {
  const response = await apiClient.put(`/users/${userId}/profile`, data);
  return response.data.profile || response.data;
}

export interface ProfileCompletion {
  percentage: number;
  missing: string[];
}

export async function calculateProfileCompletion(userId: number): Promise<ProfileCompletion> {
  const response = await apiClient.get(`/users/${userId}/profile/completion`);
  return response.data;
}

