import { MedPaciente, MedProfissional, MedAgendamento, MedProntuario, MedFinanceiro, MedNotificacao } from '../types.ts'; const API_BASE_URL = '/api/med'; // New namespace for medical API export class MedicoBackend { // --- STORAGE KEYS --- private static KEYS = { PACIENTES: 'med_pacientes', PROFISSIONAIS: 'med_profissionais', AGENDAMENTOS: 'med_agendamentos', PRONTUARIOS: 'med_prontuarios', FINANCEIRO: 'med_financeiro', NOTIFICACOES: 'med_notificacoes', AUTH: 'med_auth_token' }; // --- HELPERS --- private static async request(path: string, options: RequestInit = {}): Promise { const token = localStorage.getItem(this.KEYS.AUTH); const headers = { 'Content-Type': 'application/json', ...(token ? { 'Authorization': `Bearer ${token}` } : {}), ...options.headers, }; try { const response = await fetch(`${API_BASE_URL}${path}`, { ...options, headers }); if (!response.ok) throw new Error(`API Error: ${response.statusText}`); return await response.json(); } catch (err) { console.warn(`API unavailable at ${path}, falling back to local storage.`, err); throw err; // Let the caller decide how to handle fallback } } // --- PACIENTES --- static async getPacientes(): Promise { try { return await this.request('/pacientes'); } catch { const stored = localStorage.getItem(this.KEYS.PACIENTES); return stored ? JSON.parse(stored) : []; } } static async savePaciente(paciente: MedPaciente): Promise { try { await this.request('/pacientes', { method: 'POST', body: JSON.stringify(paciente) }); } catch { const pacientes = await this.getPacientes(); const index = pacientes.findIndex(p => p.id === paciente.id); if (index >= 0) pacientes[index] = paciente; else pacientes.push(paciente); localStorage.setItem(this.KEYS.PACIENTES, JSON.stringify(pacientes)); } } // --- AGENDA / PROFISSIONAIS --- static async getProfissionais(): Promise { try { return await this.request('/profissionais'); } catch { const stored = localStorage.getItem(this.KEYS.PROFISSIONAIS); if (stored) return JSON.parse(stored); // Mock initial data if empty const mock: MedProfissional[] = [ { id: '1', nome: 'Dr. Ricardo Santos', crm: '12345-MS', especialidade: 'Cardiologia', email: 'ricardo@med.com', telefone: '67 9999-9999', ativo: true, corAgenda: '#22d3ee' }, { id: '2', nome: 'Dra. Ana Oliveira', crm: '67890-MS', especialidade: 'Pediatria', email: 'ana@med.com', telefone: '67 8888-8888', ativo: true, corAgenda: '#f43f5e' } ]; localStorage.setItem(this.KEYS.PROFISSIONAIS, JSON.stringify(mock)); return mock; } } static async getAgendamentos(): Promise { try { return await this.request('/agendamentos'); } catch { const stored = localStorage.getItem(this.KEYS.AGENDAMENTOS); return stored ? JSON.parse(stored) : []; } } static async saveAgendamento(agenda: MedAgendamento): Promise { try { await this.request('/agendamentos', { method: 'POST', body: JSON.stringify(agenda) }); } catch { const data = await this.getAgendamentos(); const index = data.findIndex(a => a.id === agenda.id); if (index >= 0) data[index] = agenda; else data.push(agenda); localStorage.setItem(this.KEYS.AGENDAMENTOS, JSON.stringify(data)); } } // --- FINANCEIRO --- static async getFinanceiro(): Promise { try { return await this.request('/financeiro'); } catch { const stored = localStorage.getItem(this.KEYS.FINANCEIRO); return stored ? JSON.parse(stored) : []; } } // --- NOTIFICACOES --- static async getNotificacoes(): Promise { try { return await this.request('/notificacoes'); } catch { const stored = localStorage.getItem(this.KEYS.NOTIFICACOES); return stored ? JSON.parse(stored) : []; } } static async markNotificationRead(id: string): Promise { try { await this.request(`/notificacoes/${id}/read`, { method: 'PUT' }); } catch { const notifs = await this.getNotificacoes(); const index = notifs.findIndex(n => n.id === id); if (index >= 0) { notifs[index].lida = true; localStorage.setItem(this.KEYS.NOTIFICACOES, JSON.stringify(notifs)); } } } // --- AUTH --- static isAuthenticated(): boolean { return !!localStorage.getItem(this.KEYS.AUTH); } static login(token: string) { localStorage.setItem(this.KEYS.AUTH, token); } static logout() { localStorage.removeItem(this.KEYS.AUTH); } }