import { request } from '../../lib/api' import type { RClient, RClientDetail, RClientList, RContact } from '../../lib/types' const NS = '/recruiting' const body = (v: unknown) => ({ body: JSON.stringify(v) }) export const clientsApi = { list: (p: { status?: string; q?: string; owner?: string; limit?: number; offset?: number }) => { const s = new URLSearchParams() if (p.status) s.set('status', p.status) if (p.q) s.set('q', p.q) if (p.owner) s.set('owner', p.owner) if (p.limit) s.set('limit', String(p.limit)) if (p.offset) s.set('offset', String(p.offset)) const qs = s.toString() return request(`${NS}/clients${qs ? `?${qs}` : ''}`) }, get: (id: string) => request(`${NS}/clients/${encodeURIComponent(id)}`), create: (v: { name: string; uei?: string; industry?: string; website?: string; status?: string; notes?: string }) => request(`${NS}/clients`, { method: 'POST', ...body(v) }), // expected_updated_at is required by the server. Sending the value read with the row is what turns a // lost update into a refusal. update: (id: string, v: Record) => request(`${NS}/clients/${encodeURIComponent(id)}`, { method: 'PATCH', ...body(v) }), claim: (id: string) => request(`${NS}/clients/${encodeURIComponent(id)}/claim`, { method: 'POST' }), release: (id: string) => request(`${NS}/clients/${encodeURIComponent(id)}/release`, { method: 'POST' }), reassign: (id: string, owner_user_id: string) => request(`${NS}/clients/${encodeURIComponent(id)}/reassign`, { method: 'POST', ...body({ owner_user_id }) }), addContact: (clientId: string, v: Record) => request(`${NS}/clients/${encodeURIComponent(clientId)}/contacts`, { method: 'POST', ...body(v) }), updateContact: (id: string, v: Record) => request(`${NS}/contacts/${encodeURIComponent(id)}`, { method: 'PATCH', ...body(v) }), }