import { apiFetch } from './client';

export type EnumOption = { value: string; label: string };
export type RhythmOption = EnumOption & { description: string };
export type ToneOption = EnumOption & { description: string };

export type ProfileSummary = {
  calorieTarget: number | null;
  noGoalChosen: boolean;
  hydrationTarget: number | null;
  rhythmTargets: Record<string, number>;
  waterTrackingEnabled: boolean;
  suggestMealsToFamily: boolean;
  dieticianTone: string;
};

export type Profile = ProfileSummary & {
  firstname: string | null;
  email: string;
  sexe: string | null;
  tailleCm: number | null;
  poidsKg: number | null;
  age: number | null;
  poidsObjectifKg: number | null;
  niveauActivite: string | null;
  rythmeCalorique: string | null;
  customCalorieTargetKcal: number | null;
  sexes: EnumOption[];
  niveauxActivite: EnumOption[];
  rythmesCaloriques: RhythmOption[];
  dieticianTones: ToneOption[];
};

export type ProfileUpdateInput = {
  sexe: string | null;
  tailleCm: number | null;
  poidsKg: number | null;
  age: number | null;
  poidsObjectifKg: number | null;
  niveauActivite: string | null;
  rythmeCalorique: string | null;
  customCalorieTargetKcal: number | null;
  dieticianTone: string;
};

export function fetchProfile() {
  return apiFetch<Profile>('/profile');
}

export function updateProfile(input: ProfileUpdateInput) {
  return apiFetch<Profile>('/profile', { method: 'POST', body: input });
}

export function setWaterTrackingEnabled(enabled: boolean) {
  return apiFetch<{ enabled: boolean }>('/profile/water-tracking', { method: 'POST', body: { enabled } });
}

export function setSuggestMealsToFamily(enabled: boolean) {
  return apiFetch<{ enabled: boolean }>('/profile/suggest-meals-to-family', { method: 'POST', body: { enabled } });
}
