import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getSessions, getSessionById } from '../api/sessions'

export const useSessionsStore = defineStore('sessions', () => {
  const list = ref([])
  const total = ref(0)
  const currentSession = ref(null)

  async function fetchSessions(params = {}) {
    const res = await getSessions(params)
    list.value = res.data?.data ?? []
    total.value = res.data?.total ?? 0
    return { list: list.value, total: total.value }
  }

  async function fetchSessionDetail(id) {
    const res = await getSessionById(id)
    currentSession.value = res.data ?? null
    return currentSession.value
  }

  function clearCurrentSession() {
    currentSession.value = null
  }

  return {
    list,
    total,
    currentSession,
    fetchSessions,
    fetchSessionDetail,
    clearCurrentSession,
  }
})
