import { defineStore } from 'pinia' import { ref, computed } from 'vue' import { propertiesApi } from '@/services/api' import { useAuthStore } from './auth' import type { Property } from '@/types' export const usePropertyStore = defineStore('property', () => { const properties = ref([]) const currentPropertyId = ref( localStorage.getItem('currentPropertyId') ? Number(localStorage.getItem('currentPropertyId')) : null ) const loading = ref(false) const currentProperty = computed(() => properties.value.find(p => p.id === currentPropertyId.value) || null ) const setCurrentProperty = (id: number | null) => { currentPropertyId.value = id if (id) { localStorage.setItem('currentPropertyId', String(id)) } else { localStorage.removeItem('currentPropertyId') } } const fetchMyProperties = async () => { loading.value = true try { const authStore = useAuthStore() if (authStore.isSuperadmin) { properties.value = await propertiesApi.listAll() } else { properties.value = await propertiesApi.list() } // Auto-select first property if none selected if (!currentPropertyId.value && properties.value.length > 0) { setCurrentProperty(properties.value[0].id) } } finally { loading.value = false } } return { properties, currentPropertyId, currentProperty, loading, setCurrentProperty, fetchMyProperties } })