import { request } from '../../lib/api' import type { RRequisition, RRequisitionDetail, RRequisitionList } from '../../lib/types' const NS = '/recruiting' const body = (v: unknown) => ({ body: JSON.stringify(v) }) export const requisitionsApi = { list: (p: { client_id?: string; status?: string; owner?: string; q?: string; limit?: number; offset?: number }) => { const s = new URLSearchParams() if (p.client_id) s.set('client_id', p.client_id) if (p.status) s.set('status', p.status) if (p.owner) s.set('owner', p.owner) if (p.q) s.set('q', p.q) 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}/requisitions${qs ? `?${qs}` : ''}`) }, get: (id: string) => request(`${NS}/requisitions/${encodeURIComponent(id)}`), create: (v: Record) => request(`${NS}/requisitions`, { method: 'POST', ...body(v) }), update: (id: string, v: Record) => request(`${NS}/requisitions/${encodeURIComponent(id)}`, { method: 'PATCH', ...body(v) }), // Status is its own endpoint, not a PATCH field. It moves clocks and writes history, and the server // decides from the transition graph whether the move is legal at all. changeStatus: (id: string, v: { expected_updated_at: string; status: string; note?: string }) => request(`${NS}/requisitions/${encodeURIComponent(id)}/status`, { method: 'POST', ...body(v) }), claim: (id: string) => request(`${NS}/requisitions/${encodeURIComponent(id)}/claim`, { method: 'POST' }), release: (id: string) => request(`${NS}/requisitions/${encodeURIComponent(id)}/release`, { method: 'POST' }), }