/*
|--------------------------------------------------------------------------
| Agência PSI
|--------------------------------------------------------------------------
| Arquivo: src/features/patients/composables/usePatients.js
| V#3 — composable que agrega estado reativo (rows/loading/error) e delega
| toda I/O ao patientsRepository. Mesmo padrão de useAgendaEvents.
|--------------------------------------------------------------------------
*/
import { ref } from 'vue';
import {
    listPatients,
    getPatientById,
    createPatient,
    updatePatient,
    softDeletePatient
} from '@/features/patients/services/patientsRepository';

export function usePatients() {
    const rows = ref([]);
    const loading = ref(false);
    const error = ref(null);

    async function load(opts) {
        loading.value = true;
        error.value = null;
        try {
            rows.value = await listPatients(opts);
        } catch (e) {
            error.value = e?.message || 'Erro ao carregar pacientes';
            rows.value = [];
        } finally {
            loading.value = false;
        }
    }

    async function getById(id, opts) {
        return getPatientById(id, opts);
    }

    async function create(payload) {
        return createPatient(payload);
    }

    async function update(id, patch, opts) {
        return updatePatient(id, patch, opts);
    }

    async function remove(id, opts) {
        await softDeletePatient(id, opts);
    }

    return { rows, loading, error, load, getById, create, update, remove };
}
