import { defineStore } from 'pinia' import { ref } from 'vue' import { useApi } from '../composables/useApi.js' import { useAsyncAction } from '../composables/useAsyncAction.js' import { toast } from '../utils/toast.js' // Backs the subscription-roster card (#387 C3). The whole reason this surface // exists: a roster that never synced, or failed to, must be DISTINGUISHABLE // from an account that subscribes to nothing. All three states look like zero // rows, and only one of them means "you are tracking things you do not pay // for" — so the UI must never render "never synced" as a number. export const useMembershipSyncStore = defineStore('membershipSync', () => { const api = useApi() const platforms = ref([]) const { loading, error, run } = useAsyncAction({ errorAs: 'message' }) async function load () { await run(async () => { const body = await api.get('/api/sources/membership-sync') platforms.value = body.platforms || [] }) } async function syncNow () { try { await api.post('/api/sources/membership-sync', {}) toast({ text: 'Roster sync queued — runs in the background; reload to see the result.', type: 'info' }) } catch (e) { toast({ text: `Sync failed to queue: ${e.message}`, type: 'error' }) } } return { platforms, loading, error, load, syncNow } })