import { defineStore } from 'pinia'
import { ref } from 'vue'
import { fetchGatewayFleet, fetchGatewayDetail } from '@/api/operations'

export const useOperationsStore = defineStore('operations', () => {
  const fleet = ref([])
  const selectedGateway = ref(null)
  const loading = ref(false)
  const error = ref(null)
  const statusFilter = ref(null)

  async function fetchFleet() {
    loading.value = true
    error.value = null
    try {
      fleet.value = await fetchGatewayFleet(statusFilter.value)
    } catch (e) {
      error.value = e.message
    } finally {
      loading.value = false
    }
  }

  async function fetchGatewayDetail(id) {
    loading.value = true
    error.value = null
    try {
      selectedGateway.value = await fetchGatewayDetail(id)
    } catch (e) {
      error.value = e.message
    } finally {
      loading.value = false
    }
  }

  function setStatusFilter(status) {
    statusFilter.value = status
    fetchFleet()
  }

  return { fleet, selectedGateway, loading, error, statusFilter, fetchFleet, fetchGatewayDetail, setStatusFilter }
})