const BASE = "/api/public"; export interface PublicBusiness { id: number; name: string; industry: string; currency_symbol: string; phone: string | null; address: string | null; booking_enabled: boolean; cancel_window_hours: number; cancel_penalty_pct: number; require_deposit: boolean; deposit_pct: number; } export interface PublicService { id: number; name: string; description: string | null; category: string; duration_min: number; price: number; color: string; } export interface PublicEmployee { id: number; name: string; role: string; color: string; } export interface PublicBusinessResponse { business: PublicBusiness; services: PublicService[]; employees: PublicEmployee[]; } export interface PublicSlot { time: string; iso: string; employee_id: number; employee_name: string; } export interface PublicSlotsResponse { slots: PublicSlot[]; service: Pick; } export interface BookClient { name: string; email?: string; phone?: string; notes?: string; } export interface BookPayload { service_id: number; employee_id: number; start_at: string; client: BookClient; } export interface BookResponse { appointment: { id: number; start_at: string; price: number; service_name: string; employee_name: string; }; business: { name: string; currency_symbol: string }; client_created: boolean; } async function request(path: string, init?: RequestInit): Promise { const res = await fetch(`${BASE}${path}`, { ...init, headers: { "Content-Type": "application/json", ...(init?.headers as Record | undefined), }, }); if (!res.ok) { let message = `Error ${res.status}`; try { const body = await res.json(); if (body?.error) message = body.error; } catch { /* ignore */ } throw new Error(message); } return (await res.json()) as T; } export function getBusiness(slug: string): Promise { return request(`/${slug}`); } export function getSlots( slug: string, serviceId: number, date: string, employeeId?: number ): Promise { const q = new URLSearchParams({ service_id: String(serviceId), date }); if (employeeId) q.set("employee_id", String(employeeId)); return request(`/${slug}/slots?${q.toString()}`); } export function book(slug: string, payload: BookPayload): Promise { return request(`/${slug}/book`, { method: "POST", body: JSON.stringify(payload), }); }