import apiEndpoints from '../apiEndpoints';

interface CreateUserResponse {
  message: string;
}

export const createUser = async (
  fullName: string,
  username: string,
  email: string,
  password: string,
  address: string,
  address2: string,
  city: string,
  country: string,
  postcode: string
): Promise<CreateUserResponse> => {
  const response = await fetch(
    `http://localhost:3000${apiEndpoints.account.createUser}`,
    {
      method: 'POST',
      body: JSON.stringify({
        fullName,
        username,
        email,
        password,
        address,
        address2,
        city,
        country,
        postcode,
      }),
      headers: {
        'Content-Type': 'application/json',
      },
    }
  );

  const data: CreateUserResponse = await response.json();

  if (!response.ok) {
    throw new Error(data.message || 'Something went wrong!');
  }

  return data;
};
