/* |-------------------------------------------------------------------------- | Agência PSI |-------------------------------------------------------------------------- | Arquivo: src/features/patients/composables/usePatientDocuments.js | | Documentos do paciente — carrega so os campos pra KPIs (count, tipo, | ultima atualizacao). O detalhe completo fica em DocumentsListPage que | tem composable proprio. Filtra deletados (deleted_at IS NULL). |-------------------------------------------------------------------------- */ import { ref, computed } from 'vue'; import { supabase } from '@/lib/supabase/client'; export function usePatientDocuments() { const documents = ref([]); const loading = ref(false); const error = ref(''); async function load(patientId) { if (!patientId) { documents.value = []; return; } loading.value = true; error.value = ''; documents.value = []; try { const { data, error: err } = await supabase .from('documents') .select('id, tipo_documento, created_at, status_revisao, tamanho_bytes') .eq('patient_id', patientId) .is('deleted_at', null) .order('created_at', { ascending: false }) .limit(200); if (err) throw err; documents.value = data || []; } catch (e) { error.value = e?.message || 'Falha ao carregar documentos.'; documents.value = []; } finally { loading.value = false; } } const total = computed(() => documents.value.length); const totalBytes = computed(() => documents.value.reduce((acc, d) => acc + Number(d.tamanho_bytes || 0), 0) ); const tiposCount = computed(() => { const map = new Map(); documents.value.forEach((d) => { const k = d.tipo_documento || 'outro'; map.set(k, (map.get(k) || 0) + 1); }); return Object.fromEntries(map); }); const ultimo = computed(() => documents.value[0] || null); return { documents, loading, error, load, total, totalBytes, tiposCount, ultimo }; }