import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getProfiles } from '@/api/index.js'

export const useProfilesStore = defineStore('profiles', () => {
  const profiles = ref([])
  const selectedProfileId = ref(null)
  const loading = ref(false)

  async function fetchProfiles() {
    loading.value = true
    try {
      profiles.value = await getProfiles()
      if (profiles.value.length && !selectedProfileId.value) {
        selectedProfileId.value = profiles.value[0].id
      }
    } finally {
      loading.value = false
    }
  }

  function getProfile(id) {
    return profiles.value.find(p => p.id === id) ?? null
  }

  return { profiles, selectedProfileId, loading, fetchProfiles, getProfile }
})
