import { apiFetch } from './client';

export type FamilyMember = {
  id: number;
  name: string;
  email: string;
  isCreator: boolean;
  isSelf: boolean;
};

export type Family = {
  isCreator: boolean;
  creatorName: string;
  invitationCode: string | null;
  members: FamilyMember[];
};

export type Account = {
  firstname: string | null;
  email: string;
  licence: { value: string; label: string };
  family: Family | null;
};

export function fetchAccount() {
  return apiFetch<Account>('/account');
}

export function generateInvitationCode() {
  return apiFetch<Account>('/account/code', { method: 'POST' });
}

export function joinFamily(code: string) {
  return apiFetch<Account>('/account/join', { method: 'POST', body: { code } });
}

export function inviteMember(email: string) {
  return apiFetch<Account>('/account/invite', { method: 'POST', body: { email } });
}

export function leaveFamily() {
  return apiFetch<Account>('/account/leave', { method: 'POST' });
}

// En POST plutôt que DELETE — l'hébergement de prod bloque ce verbe HTTP (403 avant
// même d'atteindre PHP), voir Api\V1\TrackingController::removeEntry().
export function removeMember(id: number) {
  return apiFetch<Account>(`/account/members/${id}/remove`, { method: 'POST' });
}
