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 reconciliation card (#387 C4). Two directions with deliberately // different weights: adopting a creator you already pay for is a per-row // action, while "you follow this and your roster doesn't show it" only ever // REPORTS — the operator disables from the Subscriptions row itself. // // `fresh` is the field that matters most here. A stale or never-run roster // returns an empty tracked_not_subscribed list, and the card must say WHY it is // empty rather than letting it read as "everything is fine". export const useMembershipReconcileStore = defineStore('membershipReconcile', () => { 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/reconciliation') platforms.value = body.platforms || [] }) } async function adopt (membershipId) { try { const res = await api.post('/api/sources/reconciliation/adopt', { membership_id: membershipId }) toast({ text: res.already_tracked ? 'Already tracked — nothing to add' : 'Now tracking this creator', type: 'success' }) // Reload rather than splice: adopting moves the row from one bucket to // another, and the counts beside them have to move with it. await load() } catch (e) { toast({ text: `Could not add: ${e?.body?.detail || e.message}`, type: 'error' }) } } return { platforms, loading, error, load, adopt } })