import axios from "axios";

const BASE_URL = "http://192.168.1.64:3001";

export const loginUser = async (mobile) => {
  const res = await axios.get(`${BASE_URL}/users`, {
    params: { Mobile: mobile }
  });
  console.log("Login User API response:", res.data); 
  return res.data.length ? res.data[0] : null;
};

export const saveSeat=async({userId,seats})=>{
  const response=await axios.post(`${BASE_URL}/seats`,{
userId,
seats,
status:'booked',
  });
  return response.data;
 
}

export const getSeat=async()=>{
  const response= await axios.get(`${BASE_URL}/seats`);
  return response.data;
}

export const deleteSeat=async(id)=>{
  const response=await axios.delete(`${BASE_URL}/seats/${id}`,);
  return response.data;
}

export const getSeatLayout=async()=>{
  const response=await axios.get(`${BASE_URL}/seatLayout`);

  return response.data;
}

export const getReservedSeats=async()=>{
  const response=await axios.get(`${BASE_URL}/seats`);
  const allSeats=response.data;

  const bookedIDs=allSeats
  .filter((seatObj)=>seatObj.status === 'booked').flatMap((seatObj)=>seatObj.seats);
return bookedIDs
}

export const register = async ({ mobile,name }) => {
  const response = await axios.post(`${BASE_URL}/users`, {id:mobile, Mobile: mobile,Name:name, });
  return response.data;
};

export const updateSeat = async ({ id, seats,userId}) => {
  const response = await axios.put(`${BASE_URL}/seats/${id}`, { userId, seats,status:"booked" });
  return response.data;
};
export const getUserReservedSeats = async (userId) => {
  const response = await axios.get(`${BASE_URL}/seats`, {
    params: { userId },
  });
   const record=response.data.find(seatObj=> seatObj.status === 'booked');
   return record || null;
    
};
