import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'
import { useAsyncAction } from '../composables/useAsyncAction.js'

// Backs the Series browse view (FC-6.2). list_series returns every series as a
// card; homelab scale is small enough to load the whole list and sort/filter
// server-side.
export const useSeriesBrowseStore = defineStore('seriesBrowse', () => {
  const api = useApi()
  const series = ref([])
  const sort = ref('recent')
  const artistId = ref(null)
  const { loading, error, run } = useAsyncAction({ errorAs: 'message' })

  async function load() {
    await run(async () => {
      const params = { sort: sort.value }
      if (artistId.value != null) params.artist_id = artistId.value
      const body = await api.get('/api/series', { params })
      series.value = body.series || []
    })
  }

  function setSort(s) {
    sort.value = s
    return load()
  }

  // A series IS a Tag(kind=series); deleting it removes the series structure
  // (pages/chapters cascade) via the shared admin tag-delete. Splice the card
  // out for instant feedback rather than reloading the whole grid.
  async function remove(id) {
    await api.delete(`/api/admin/tags/${id}`)
    series.value = series.value.filter(s => s.id !== id)
  }

  // Reflect an in-place rename (PATCH /api/tags/<id>) without a full reload.
  function applyRename(id, name) {
    const row = series.value.find(s => s.id === id)
    if (row) row.name = name
  }

  return {
    series, sort, artistId, loading, error,
    load, setSort, remove, applyRename,
  }
})
