/* |-------------------------------------------------------------------------- | Agência PSI |-------------------------------------------------------------------------- | Arquivo: src/features/patients/composables/usePatientDetail.js | | Detalhe completo de paciente — patient row + grupos + tags. Extraido do | PatientProntuario.vue pra permitir reuso em MelissaPaciente.vue. | Mantem a mesma logica (Promise.all em 2 etapas) — agora via repository. |-------------------------------------------------------------------------- */ import { ref } from 'vue'; import { getPatientById, getPatientRelations, getGroupsByIds, getTagsByIds } from '@/features/patients/services/patientsRepository'; export function usePatientDetail() { const patient = ref(null); const groups = ref([]); const tags = ref([]); const loading = ref(false); const loadError = ref(''); async function load(id) { if (!id) { patient.value = null; groups.value = []; tags.value = []; return; } loading.value = true; loadError.value = ''; patient.value = null; groups.value = []; tags.value = []; try { const [p, rel] = await Promise.all([getPatientById(id), getPatientRelations(id)]); if (!p) throw new Error('Paciente não retornou dados (RLS bloqueando ou ID não existe).'); patient.value = p; const [g, t] = await Promise.all([ getGroupsByIds(rel.groupIds || []), getTagsByIds(rel.tagIds || []) ]); groups.value = g; tags.value = t; } catch (e) { loadError.value = e?.message || 'Falha ao buscar dados.'; } finally { loading.value = false; } } return { patient, groups, tags, loading, loadError, load }; }