import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useButterfliuStore = defineStore('butterfliu', () => {
  const libraries = ref([])
  async function fetchLibraries() {
    libraries.value = await (await fetch('/api/libraries')).json()
  }

  async function scanLibrary(id) {
    const resp = await fetch('/api/libraries/' + id + '/scan', {
      method: 'POST',
    })
    const result = await resp.json()

    console.log(result)
  }
  async function addLibrary(name, path) {
    const resp = await fetch('/api/libraries', {
      method: 'POST',
      body: JSON.stringify({
        name,
        path,
      }),
    })
    const result = await resp.json()

    console.log(result)
  }

  async function deleteLibrary(id) {
    const resp = await fetch('/api/libraries/' + id, {
      method: 'DELETE',
    })
    const result = await resp.json()

    console.log(result)
  }

  async function fetchLibrarySongs(id) {
    const resp = await fetch('/api/libraries/' + id + '/songs')
    const songs = await resp.json()
    return songs
  }

  async function fetchArtists() {
    const resp = await fetch('/api/artists')
    const artists = await resp.json()
    return artists
  }

  async function fetchAlbums() {
    const resp = await fetch('/api/albums')
    const albums = await resp.json()
    return albums
  }

  async function fetchAllSongs() {
    const resp = await fetch('/api/songs')
    const songs = await resp.json()
    return songs
  }

  return { libraries, fetchLibraries, scanLibrary, addLibrary, deleteLibrary, fetchLibrarySongs, fetchArtists, fetchAlbums, fetchAllSongs }
})
