import apiFetch from "./apiFetch";

export const getMusicians = (page) => {
  return new Promise((resolve, reject) => {
    apiFetch(`/search/musicians?results=true`)
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

// export const getPeople = (page) => {
//   return new Promise((resolve, reject) => {
//     apiFetch(`/filter?page=${page}`)
//     .then(response => resolve(response))
//     .catch(error => reject(error))
//   })
// }

export const getPersonById = (id) => {
  return new Promise((resolve, reject) => (
    apiFetch(`/users/${id}/profile?show_teacher=true`)
    .then(response => resolve(response))
    .catch(error => reject(error))
  ))
}

export const getPeople = ({ data, offset, limit } = {}) => {
  return new Promise((resolve, reject) => {
    apiFetch(`/filter?offset=${offset}&limit=${limit}`, {
      method: 'POST',
      body: JSON.stringify(data) 
    })
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

export const getGenres = () => {
  return new Promise((resolve, reject) => {
    apiFetch('/genres')
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

export const getInstruments = () => {
  return new Promise((resolve, reject) => {
    apiFetch('/instruments')
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

// export const getCurrentUser = () => {
//   return new Promise((resolve, reject) => {
//     apiFetch('/me')
//     .then(response => resolve(response))
//     .catch(error => reject(error))
//   })
// }

export const getFriends = (userId) => {
  return new Promise((resolve, reject) => {
    apiFetch(`/users/${userId}/friends`)
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

export const addFriend = (userId, friendId) => {
  return new Promise((resolve, reject) => {
    apiFetch(`/users/${userId}/friend_requests`, {
      method: 'POST',
      body: JSON.stringify({ friend_id: friendId })
    })
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

export const removeFriend = (userId, friendId) => {
  return new Promise((resolve, reject) => {
    apiFetch(`/users/${userId}/friends/${friendId}`, {
      method: 'DELETE'
    })
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

export const getTextMessages = (options = {}) => {
  return new Promise((resolve, reject) => {
    apiFetch(`/text_messages?${new URLSearchParams(options)}`)
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

export const createTextMessage = (options) => {
  return new Promise((resolve, reject) => {
    apiFetch(`/text_messages`, {
      method: "POST",
      body: JSON.stringify(options)
    })
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

export const getNotifications = (userId, options = {}) => {
  return new Promise((resolve, reject) => {
    apiFetch(`/users/${userId}/notifications?${new URLSearchParams(options)}`)
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
  
}

export const acceptFriendRequest = (userId, options = {}) => {
  return new Promise((resolve, reject) => {
    const { status, friend_request_id } = options
    apiFetch(`/users/${userId}/friend_requests/${friend_request_id}`, {
      method: 'POST',
      body: JSON.stringify({ status })
    })
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

export const deleteNotification = (userId, notificationId) => {
  return new Promise((resolve, reject) => {
    apiFetch(`/users/${userId}/notifications/${notificationId}`, {
      method: 'DELETE',
    })
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}

export const getSessions = () => {
  return new Promise((resolve, reject) => {
    apiFetch(`/sessions`)
    .then(response => resolve(response))
    .catch(error => reject(error))
  }) 
}

export const getLatencyToUsers = (currentUserId, participantIds) => {
  return new Promise((resolve, reject) => {
    const options = { user_ids: [participantIds]} 
    apiFetch(`/users/${currentUserId}/latencies?${new URLSearchParams(options)}`)
    .then(response => resolve(response))
    .catch(error => reject(error))
  })
}
