const API_URL = typeof window !== 'undefined' ? (process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001') : (process.env.API_URL || 'http://localhost:3001'); export interface Worker { id: string; name: string; presenceDays: number; } export interface Settings { id: number; totalPoints: number; totalDays: number; } export const workersAPI = { async getAll(): Promise { const response = await fetch(`${API_URL}/workers`); if (!response.ok) throw new Error('Failed to fetch workers'); return response.json(); }, async create(worker: Omit): Promise { const response = await fetch(`${API_URL}/workers`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(worker), }); if (!response.ok) throw new Error('Failed to create worker'); return response.json(); }, async update(id: string, worker: Partial): Promise { const response = await fetch(`${API_URL}/workers/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(worker), }); if (!response.ok) throw new Error('Failed to update worker'); return response.json(); }, async delete(id: string): Promise { const response = await fetch(`${API_URL}/workers/${id}`, { method: 'DELETE', }); if (!response.ok) throw new Error('Failed to delete worker'); }, async resetAllPresences(): Promise { const workers = await this.getAll(); await Promise.all( workers.map(worker => this.update(worker.id, { presenceDays: 0 }) ) ); }, }; export const settingsAPI = { async get(): Promise { const response = await fetch(`${API_URL}/settings`); if (!response.ok) throw new Error('Failed to fetch settings'); return response.json(); }, async update(settings: Partial): Promise { const response = await fetch(`${API_URL}/settings`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(settings), }); if (!response.ok) throw new Error('Failed to update settings'); return response.json(); }, };