import type { ClaimRow } from './claims.definition'
import { kbxApi } from '../../../http/generated/kbxApiClient'

export interface ClaimSearch { from?: string; to?: string; type?: string; status?: string; keyword?: string }

const transitionOperations = {
  approve: 'oms.claims.approve',
  hold: 'oms.claims.hold',
  start: 'oms.claims.start',
  complete: 'oms.claims.complete',
} as const

export const claimsApi = {
  search(filter: ClaimSearch) {
    return kbxApi.request<{ items: ClaimRow[]; totalCount: number }>('oms.claims.search', { query: filter })
  },
  transition(id: string, transition: string) {
    const operation = transitionOperations[transition as keyof typeof transitionOperations]
    if (!operation) throw new Error(`Unsupported claim transition: ${transition}`)
    return kbxApi.request<{ id: string; status: string; version: number }>(operation, { path: { id } })
  },
}
