import { useState, useCallback } from 'react'; import { Models } from 'appwrite'; import { pagamentoService, PagamentoData } from '@/services/pagamentoService'; export type { PagamentoData }; export interface UsePagamentosReturn { pagamentos: Models.Document[]; loading: boolean; error: string | null; totalPagamentos: number; currentPage: number; listarPagamentos: (page?: number, search?: string) => Promise; buscarPagamentos: (termo: string, page?: number) => Promise; cadastrarPagamento: (data: PagamentoData) => Promise; atualizarPagamento: (id: string, data: PagamentoData) => Promise; deletarPagamento: (id: string) => Promise; setCurrentPage: (page: number) => void; } const PAGE_SIZE = 10; export const usePagamentos = (): UsePagamentosReturn => { const [pagamentos, setPagamentos] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [totalPagamentos, setTotalPagamentos] = useState(0); const [currentPage, setCurrentPage] = useState(1); const listarPagamentos = useCallback( async (page = currentPage, search = '') => { setLoading(true); setError(null); try { const response = await pagamentoService.listar(page, PAGE_SIZE, search); setPagamentos(response.documents || []); setTotalPagamentos(response.total || 0); setCurrentPage(page); } catch (err) { setError('Erro de conexão ao carregar pagamentos'); } finally { setLoading(false); } }, [currentPage]); const buscarPagamentos = useCallback( async (termo: string, page = 1) => { setLoading(true); setError(null); try { const response = await pagamentoService.buscarPorNome( termo, page, PAGE_SIZE ); setPagamentos(response.documents || []); setTotalPagamentos(response.total || 0); setCurrentPage(page); } catch (err) { setError('Erro de conexão ao buscar pagamentos'); } finally { setLoading(false); } }, [] ); const cadastrarPagamento = useCallback(async (formData: PagamentoData): Promise => { setLoading(true); setError(null); try { await pagamentoService.criar(formData); await listarPagamentos(currentPage); return true; } catch (err) { setError('Erro de conexão ao cadastrar pagamento'); return false; } finally { setLoading(false); } }, [currentPage, listarPagamentos]); const atualizarPagamento = useCallback(async (id: string, formData: PagamentoData): Promise => { setLoading(true); setError(null); try { await pagamentoService.atualizar(id, formData); await listarPagamentos(currentPage); return true; } catch (err) { setError('Erro de conexão ao atualizar pagamento'); return false; } finally { setLoading(false); } }, [currentPage, listarPagamentos]); const deletarPagamento = useCallback(async (id: string): Promise => { setLoading(true); setError(null); try { await pagamentoService.deletar(id); await listarPagamentos(currentPage); return true; } catch (err) { setError('Erro de conexão ao deletar pagamento'); return false; } finally { setLoading(false); } }, [currentPage, listarPagamentos]); return { pagamentos, loading, error, totalPagamentos, currentPage, listarPagamentos, buscarPagamentos, cadastrarPagamento, atualizarPagamento, deletarPagamento, setCurrentPage, }; };