import type { Client, PaginatedResponse } from '~/types' export const useClient = () => { const clients = useState('clients.list', () => []) const searchQuery = ref('') const fetchClients = async (): Promise => { const response = await useApiFetch>('/api/clients') clients.value = response.data } const search = async (q: string): Promise => { if (!q.trim()) { await fetchClients() return } const response = await useApiFetch<{ data: Client[] }>('/api/search', { query: { q }, }) clients.value = response.data } const fetchClient = async (id: number): Promise => { const response = await useApiFetch<{ data: Client }>(`/api/clients/${id}`) return response.data } const createClient = async (payload: { client_number: string; name: string; notes?: Record }): Promise => { const response = await useApiFetch<{ data: Client }>('/api/clients', { method: 'POST', body: payload, }) return response.data } const updateClient = async (id: number, payload: Partial>): Promise => { const response = await useApiFetch<{ data: Client }>(`/api/clients/${id}`, { method: 'PUT', body: payload, }) return response.data } const updateCustomFieldValue = async ( clientId: number, definitionId: number, value: string | null, ): Promise => { const response = await useApiFetch<{ data: Client }>( `/api/clients/${clientId}/custom-fields/${definitionId}`, { method: 'PUT', body: { value } }, ) return response.data } return { clients, searchQuery, fetchClients, search, fetchClient, createClient, updateClient, updateCustomFieldValue } }