const API_BASE = '/api' class ApiService { constructor() { this.token = null } setToken(token) { this.token = token } async request(endpoint, options = {}) { const headers = { 'Content-Type': 'application/json', ...options.headers, } if (this.token) { headers['Authorization'] = `Bearer ${this.token}` } const response = await fetch(`${API_BASE}${endpoint}`, { ...options, headers, }) if (!response.ok) { const error = await response.json().catch(() => ({ detail: 'Request failed' })) throw new Error(error.detail || 'Request failed') } return response.json() } // Grid endpoints async getGrid(system = 'case33bw') { return this.request(`/grid?system=${system}`) } async getBaseline(system = 'case33bw') { return this.request(`/baseline/${system}`) } // Optimization async optimize(data) { return this.request('/optimize', { method: 'POST', body: JSON.stringify(data), }) } // Simulation async simulate(data) { return this.request('/simulate', { method: 'POST', body: JSON.stringify(data), }) } async toggleSwitch(data) { return this.request('/simulate/toggle', { method: 'POST', body: JSON.stringify(data), }) } // Grid - Set Out of Service Lines async setOutOfServiceLines(data) { return this.request('/grid/set-out-of-service', { method: 'POST', body: JSON.stringify(data), }) } // Digital Twin async digitalTwin(data) { return this.request('/digital-twin', { method: 'POST', body: JSON.stringify(data), }) } async getScenarios(system = 'case33bw') { return this.request(`/digital-twin/scenarios?system=${system}`) } // Usage & Stats async getUsage(limit = 100) { return this.request(`/usage?limit=${limit}`) } async getUsageStats() { return this.request('/usage/stats') } async getUsageSummary() { return this.request('/usage/summary') } // Audit async getAuditLogs(limit = 100) { return this.request(`/audit?limit=${limit}`) } async getAuditSummary() { return this.request('/audit/summary') } // ROI async calculateROI(data) { return this.request('/roi', { method: 'POST', body: JSON.stringify(data), }) } async getPricing() { return this.request('/roi/pricing') } async compareROI(numberOfFeeders) { return this.request(`/roi/comparison?number_of_feeders=${numberOfFeeders}`) } // Reports async getReportData(system = 'case33bw', method = 'hybrid') { return this.request(`/report/data?system=${system}&method=${method}`) } async generateReport(data) { const headers = { 'Content-Type': 'application/json', } if (this.token) { headers['Authorization'] = `Bearer ${this.token}` } const response = await fetch(`${API_BASE}/report`, { method: 'POST', headers, body: JSON.stringify(data), }) return response.text() } } export const api = new ApiService() export default api