// lib/api/pacientes.ts import { API_BASE, REST, baseHeaders, rangeHeaders, withPrefer, parse } from "../api"; // ===== TIPOS DE PACIENTES ===== export type Endereco = { cep?: string; logradouro?: string; numero?: string; complemento?: string; bairro?: string; cidade?: string; estado?: string; }; export type Paciente = { id: string; nome?: string; nome_social?: string | null; cpf?: string; rg?: string | null; sexo?: string | null; data_nascimento?: string | null; telefone?: string; email?: string; endereco?: Endereco; observacoes?: string | null; foto_url?: string | null; }; export type PacienteInput = { nome: string; nome_social?: string | null; cpf: string; rg?: string | null; sexo?: string | null; data_nascimento?: string | null; telefone?: string | null; email?: string | null; endereco?: Endereco; observacoes?: string | null; }; // ===== PACIENTES (CRUD) ===== export async function listarPacientes(params?: { page?: number; limit?: number; q?: string; }): Promise { const qs = new URLSearchParams(); if (params?.q) qs.set("q", params.q); const url = `${REST}/patients${qs.toString() ? `?${qs.toString()}` : ""}`; const res = await fetch(url, { method: "GET", headers: { ...baseHeaders(), ...rangeHeaders(params?.page, params?.limit), }, }); return await parse(res); } export async function buscarPacientePorId(id: string | number): Promise { const url = `${REST}/patients?id=eq.${id}`; const res = await fetch(url, { method: "GET", headers: baseHeaders() }); const arr = await parse(res); if (!arr?.length) throw new Error("404: Paciente não encontrado"); return arr[0]; } export async function criarPaciente(input: PacienteInput): Promise { const url = `${REST}/patients`; const res = await fetch(url, { method: "POST", headers: withPrefer({ ...baseHeaders(), "Content-Type": "application/json" }, "return=representation"), body: JSON.stringify(input), }); const arr = await parse(res); return Array.isArray(arr) ? arr[0] : (arr as Paciente); } export async function atualizarPaciente(id: string | number, input: PacienteInput): Promise { const url = `${REST}/patients?id=eq.${id}`; const res = await fetch(url, { method: "PATCH", headers: withPrefer({ ...baseHeaders(), "Content-Type": "application/json" }, "return=representation"), body: JSON.stringify(input), }); const arr = await parse(res); return Array.isArray(arr) ? arr[0] : (arr as Paciente); } export async function excluirPaciente(id: string | number): Promise { const url = `${REST}/patients?id=eq.${id}`; const res = await fetch(url, { method: "DELETE", headers: baseHeaders() }); await parse(res); } // ===== PACIENTES (Extra: verificação de CPF duplicado) ===== export async function verificarCpfDuplicado(cpf: string): Promise { const clean = (cpf || "").replace(/\D/g, ""); const url = `${API_BASE}/rest/v1/patients?cpf=eq.${clean}&select=id`; const res = await fetch(url, { method: "GET", headers: baseHeaders(), }); const data = await res.json().catch(() => []); return Array.isArray(data) && data.length > 0; } // ===== Stubs para upload de arquivos de paciente ===== export async function listarAnexos(_id: string | number): Promise { return []; } export async function adicionarAnexo(_id: string | number, _file: File): Promise { return {}; } export async function removerAnexo(_id: string | number, _anexoId: string | number): Promise {} export async function uploadFotoPaciente(_id: string | number, _file: File): Promise<{ foto_url?: string; thumbnail_url?: string }> { return {}; } export async function removerFotoPaciente(_id: string | number): Promise {}