import { queryOptions } from '@tanstack/react-query'
import { api } from '@/lib/api-client'
import type { TaxExemption } from '@/types/account'
import type { PaginatedResponse, PaginationInput } from '@lunarfront/shared/schemas'

export const taxExemptionKeys = {
  all: (accountId: string) => ['accounts', accountId, 'tax-exemptions'] as const,
  list: (accountId: string, params: PaginationInput) => [...taxExemptionKeys.all(accountId), params] as const,
}

export function taxExemptionListOptions(accountId: string, params: PaginationInput) {
  return queryOptions({
    queryKey: taxExemptionKeys.list(accountId, params),
    queryFn: () => api.get<PaginatedResponse<TaxExemption>>(`/v1/accounts/${accountId}/tax-exemptions`, params),
  })
}

export const taxExemptionMutations = {
  create: (accountId: string, data: Record<string, unknown>) =>
    api.post<TaxExemption>(`/v1/accounts/${accountId}/tax-exemptions`, data),

  update: (id: string, data: Record<string, unknown>) =>
    api.patch<TaxExemption>(`/v1/tax-exemptions/${id}`, data),

  approve: (id: string) =>
    api.post<TaxExemption>(`/v1/tax-exemptions/${id}/approve`, {}),

  revoke: (id: string, reason: string) =>
    api.post<TaxExemption>(`/v1/tax-exemptions/${id}/revoke`, { reason }),
}
