import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getRooms, getRoomById } from '../api/rooms'

export const useRoomsStore = defineStore('rooms', () => {
  const list = ref([])
  const currentRoom = ref(null)

  async function fetchRooms() {
    const res = await getRooms()
    list.value = res.data ?? []
    return list.value
  }

  async function fetchRoomDetail(roomId) {
    const res = await getRoomById(roomId)
    currentRoom.value = res.data ?? null
    return currentRoom.value
  }

  function clearCurrentRoom() {
    currentRoom.value = null
  }

  return {
    list,
    currentRoom,
    fetchRooms,
    fetchRoomDetail,
    clearCurrentRoom,
  }
})
