// resources/js/stores/syncState.js
import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useSyncStore = defineStore('sync', () => {
    const syncStatus = ref({}); // Maps keys to their sync status (e.g. { 'user': 'synced', 'leads': 'stale' })
    const lastSynced = ref({}); // Maps keys to their last sync timestamp
    const errors = ref({}); // Maps keys to any sync errors

    const updateStatus = (key, status) => {
        syncStatus.value[key] = status;
        if (status === 'synced') {
            lastSynced.value[key] = new Date().toISOString();
        }
    };

    const setError = (key, error) => {
        errors.value[key] = error;
        syncStatus.value[key] = 'failed';
    };

    const getStatus = (key) => syncStatus.value[key] || 'pending';

    return {
        syncStatus,
        lastSynced,
        errors,
        updateStatus,
        setError,
        getStatus
    };
});
