const API_URL = 'http://localhost:8080';


export async function loginUser(email:string, password: string) {
    const res = await fetch(`${API_URL}/login`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ email, password }),
  });
  if (!res.ok) throw new Error('Login fehlgeschlagen');
  return res.json(); // { token: string }
}

export async function registerUser(email:string, password: string){
  const res = await fetch(`${API_URL}/register`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ email, password }),
  });
  if (!res.ok) throw new Error('Registrierung fehlgeschlagen');
  return res.json(); // { token: string }
}
    
export async function fetchUserProfile(token: string) {
  const res = await fetch(`${API_URL}/profile`, {
    headers: { 'Authorization': `Bearer ${token}` },
  });
  if (!res.ok) throw new Error('Profil konnte nicht geladen werden');
  return res.json(); // { id: number, email: string, name: string }
}