export const login = async (email) => {
  try {
    const response = await fetch(`${import.meta.env.VITE_API_URL}/users`);
    const data = await response.json();
    const user = data.find((e) => e.email === email);
    return user;
  } catch (error) {
    console.error('Login error:', error);
    throw error;
  }
};

export const createUser = async (email) => {
  try {
    const response = await fetch(`${import.meta.env.VITE_API_URL}/users`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ email })
    });
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Create user error:', error);
    throw error;
  }
};

export const getSelectedSeatsApi = async () => {
  try {
    const response = await fetch(
      `${import.meta.env.VITE_API_URL}/selectedSeats`
    );
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Get selected seats error:', error);
    throw error;
  }
};

export const editSeatsApi = async (id, body) => {
  try {
    await fetch(`${import.meta.env.VITE_API_URL}/selectedSeats/${id}`, {
      method: 'PATCH',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ selected: body })
    });
  } catch (error) {
    console.error('Edit seats error:', error);
    throw error;
  }
};

export const createSeatApi = async (body) => {
  try {
    await fetch(`${import.meta.env.VITE_API_URL}/selectedSeats`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(body)
    });
  } catch (error) {
    console.error('Create seat error:', error);
    throw error;
  }
};
