import { appConstants } from '../lib/AppConstants';

export const login = async (email) => {
  const response = await fetch(`${appConstants.API_URL_USERS}`);
  const data = await response.json();
  const user = data.find((e) => e.email == email);
  return user;
};

export const createUser = async (email) => {
  const response = await fetch(`${appConstants.API_URL_USERS}`, {
    method: 'POST',
    body: JSON.stringify({
      email: email
    })
  });
  const data = await response.json();
  return data;
};

export const getSelectedSeatsApi = async () => {
  const response = await fetch(`${appConstants.API_URL_SEATS}`);
  const data = await response.json();
  return data;
};

export const editSeatsApi = async (id, body) => {
  try {
    await fetch(`${appConstants.API_URL_SEATS}/${id}`, {
      method: 'PATCH',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ selected: body })
    });
  } catch (error) {
    console.log(error, 'error');
  }
};

export const createSeatApi = async (body) => {
  await fetch(`${appConstants.API_URL_SEATS}`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(body)
  });
};

export const saveSeatSelectionCountApi = async (id, count) => {
  await fetch(`${appConstants.API_URL_USERS}/${id}`, {
    method: 'PATCH',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ selectionCount: count })
  });
};

export const getSeatSelectionCountApi = async (id) => {
  const response = await fetch(`${appConstants.API_URL_USERS}/${id}`);
  const data = await response.json();
  return data ? data : null;
};
