import type { TimeEntryInsert, CustomerInsert, ProjectInsert } from "@emberclone/shared" const API_BASE = "/api" async function request(endpoint: string, options: RequestInit = {}): Promise { const token = localStorage.getItem("auth_token") const headers = new Headers(options.headers) if (token) { headers.set("Authorization", `Bearer ${token}`) } if (!(options.body instanceof FormData)) { headers.set("Content-Type", "application/json") } const response = await fetch(`${API_BASE}${endpoint}`, { ...options, headers, credentials: "include" }) if (!response.ok) { const errorData = await response.json().catch(() => ({})) const err: any = new Error(errorData.message || `API Error: ${response.status}`) err.status = response.status throw err } return response.json() } export const api = { async login(email: string, password: string) { const data = await request<{ token: string }>("/auth/login", { method: "POST", body: JSON.stringify({ email, password }) }) if (data?.token) { localStorage.setItem("auth_token", data.token) } return data }, async logout() { localStorage.removeItem("auth_token") return request("/auth/logout", { method: "POST" }).catch(() => {}) }, async getMe() { return request<{ id: string; email: string; name: string; role: "admin" | "user" }>("/auth/me") }, async changePassword(data: { oldPassword: string; newPassword: string }) { return request("/auth/change-password", { method: "POST", body: JSON.stringify(data) }) }, async deleteAccount(password: string) { return request("/auth/delete-account", { method: "DELETE", body: JSON.stringify({ password }) }) }, async updateProfile(data: { name: string }) { return request("/users/me", { method: "PATCH", body: JSON.stringify(data) }) }, async listTimeEntries(opts?: Record) { const query = opts ? `?${new URLSearchParams(opts).toString()}` : "" return request(`/time-entries${query}`) }, async createTimeEntry(data: Partial) { return request("/time-entries", { method: "POST", body: JSON.stringify(data) }) }, async deleteTimeEntry(id: string) { return request(`/time-entries/${id}`, { method: "DELETE" }) }, async bulkDeleteTimeEntries(ids: string[]) { return request("/time-entries/bulk-delete", { method: "POST", body: JSON.stringify({ ids }) }) }, async getRunningTimeEntry() { try { return await request("/time-entries/running") } catch (err: any) { if (err.status === 404) return null throw err } }, async startTimeEntry(data: { description: string; projectId?: string }) { return request("/time-entries/start", { method: "POST", body: JSON.stringify(data) }) }, async stopTimeEntry(id: string) { return request(`/time-entries/${id}/stop`, { method: "POST" }) }, async listCustomers() { return request("/customers") }, async createCustomer(data: Partial) { return request("/customers", { method: "POST", body: JSON.stringify(data) }) }, async importCustomersCsv(file: File) { const formData = new FormData() formData.append("file", file) return request("/customers/import", { method: "POST", body: formData }) }, async deleteCustomer(id: string) { return request(`/customers/${id}`, { method: "DELETE" }) }, async getCustomerProjects(customerId: string) { return request(`/customers/${customerId}/projects`) }, async getCustomerTimeEntries(customerId: string) { return request(`/customers/${customerId}/time-entries`) }, async listProjects(opts?: Record) { const query = opts ? `?${new URLSearchParams(opts).toString()}` : "" return request(`/projects${query}`) }, async createProject(data: Partial) { return request("/projects", { method: "POST", body: JSON.stringify(data) }) }, async updateProject(id: string, data: Partial) { return request(`/projects/${id}`, { method: "PATCH", body: JSON.stringify(data) }) }, async deleteProject(id: string) { return request(`/projects/${id}`, { method: "DELETE" }) }, async listWebhooks() { return request("/webhooks") }, async createWebhook(data: { url: string; event: string }) { return request("/webhooks", { method: "POST", body: JSON.stringify(data) }) }, async updateWebhook(id: string, data: { url?: string; event?: string }) { return request(`/webhooks/${id}`, { method: "PATCH", body: JSON.stringify(data) }) }, async deleteWebhook(id: string) { return request(`/webhooks/${id}`, { method: "DELETE" }) }, async testWebhook(id: string) { return request(`/webhooks/${id}/test`, { method: "POST" }) } }